<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是C语言基础——自定义数据类型相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-15-C语言-自定义数据类型">
<meta property="og:url" content="https://sumumm.github.io/post/f72c7684.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是C语言基础——自定义数据类型相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-15-C%E8%AF%AD%E8%A8%80-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/img/image-20220326102515821.png">
<meta property="article:published_time" content="2022-03-04T07:08:36.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.986Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV01-C语言基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-15-C%E8%AF%AD%E8%A8%80-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/img/image-20220326102515821.png">


<link rel="canonical" href="https://sumumm.github.io/post/f72c7684.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/f72c7684.html","path":"post/f72c7684.html","title":"LV01-15-C语言-自定义数据类型"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-15-C语言-自定义数据类型 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-text">一、结构体</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AE%9A%E4%B9%89%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-text">1.定义结构体</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E4%B8%80%E8%88%AC%E5%AE%9A%E4%B9%89%E6%A0%BC%E5%BC%8F"><span class="nav-text">1.1一般定义格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E5%AE%9A%E4%B9%89%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.2定义实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%9A%E4%B9%89%E7%BB%93%E6%9E%84%E4%BD%93%E5%8F%98%E9%87%8F"><span class="nav-text">2.定义结构体变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BD%BF%E7%94%A8%E7%BB%93%E6%9E%84%E4%BD%93%E5%8F%98%E9%87%8F"><span class="nav-text">3.使用结构体变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%BC%95%E7%94%A8%E5%92%8C%E8%B5%8B%E5%80%BC%E7%9A%84%E4%B8%80%E8%88%AC%E6%A0%BC%E5%BC%8F"><span class="nav-text">3.1引用和赋值的一般格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E7%BB%93%E6%9E%84%E4%BD%93%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC"><span class="nav-text">3.2结构体变量赋值</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1%E8%B5%8B%E5%80%BC%E6%96%B9%E5%BC%8F%E4%B8%80"><span class="nav-text">3.2.1赋值方式一</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2%E8%B5%8B%E5%80%BC%E6%96%B9%E5%BC%8F%E4%BA%8C"><span class="nav-text">3.2.2赋值方式二</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-3%E8%B5%8B%E5%80%BC%E6%96%B9%E5%BC%8F%E4%B8%89"><span class="nav-text">3.2.3赋值方式三</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-4%E8%B5%8B%E5%80%BC%E6%96%B9%E5%BC%8F%E5%9B%9B"><span class="nav-text">3.2.4赋值方式四</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E7%BB%93%E6%9E%84%E4%BD%93%E5%8F%98%E9%87%8F%E5%B5%8C%E5%A5%97%E8%B5%8B%E5%80%BC"><span class="nav-text">3.3结构体变量嵌套赋值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E7%BB%93%E6%9E%84%E4%BD%93%E7%9A%84%E9%95%BF%E5%BA%A6"><span class="nav-text">4.结构体的长度</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1%E8%AE%A1%E7%AE%97%E6%A0%BC%E5%BC%8F"><span class="nav-text">4.1计算格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%9C%A8%E5%86%85%E5%AD%98%E4%B8%AD%E7%9A%84%E5%AD%98%E5%82%A8"><span class="nav-text">5.在内存中的存储</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%AD%97%E8%8A%82%E5%AF%B9%E9%BD%90"><span class="nav-text">6.字节对齐</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1%E5%AD%97%E8%8A%82%E5%AF%B9%E9%BD%90%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-text">6.1字节对齐是什么？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%AD%97%E8%8A%82%E5%AF%B9%E9%BD%90%EF%BC%9F"><span class="nav-text">6.2为什么要字节对齐？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-3%E5%AF%B9%E9%BD%90%E5%8E%9F%E5%88%99%EF%BC%9F"><span class="nav-text">6.3对齐原则？</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E7%BB%93%E6%9E%84%E4%BD%93%E6%95%B0%E7%BB%84"><span class="nav-text">二、结构体数组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AE%9A%E4%B9%89%E7%BB%93%E6%9E%84%E4%BD%93%E6%95%B0%E7%BB%84"><span class="nav-text">1.定义结构体数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%BB%93%E6%9E%84%E4%BD%93%E6%95%B0%E7%BB%84%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">2.结构体数组的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">2.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E8%B5%8B%E5%80%BC%E4%B8%8E%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.2赋值与初始化</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E7%BB%93%E6%9E%84%E4%BD%93%E6%8C%87%E9%92%88"><span class="nav-text">三、结构体指针</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AE%9A%E4%B9%89%E7%BB%93%E6%9E%84%E4%BD%93%E6%8C%87%E9%92%88"><span class="nav-text">1.定义结构体指针</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%BB%93%E6%9E%84%E4%BD%93%E6%8C%87%E9%92%88%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">2.结构体指针的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F-1"><span class="nav-text">2.1使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E8%B5%8B%E5%80%BC%E4%B8%8E%E5%88%9D%E5%A7%8B%E5%8C%96-1"><span class="nav-text">2.2赋值与初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E7%BB%93%E6%9E%84%E4%BD%93%E6%8C%87%E9%92%88%E4%B8%8E%E6%95%B0%E7%BB%84"><span class="nav-text">2.3结构体指针与数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4%E7%BB%93%E6%9E%84%E4%BD%93%E6%8C%87%E9%92%88%E4%B8%8E%E5%87%BD%E6%95%B0"><span class="nav-text">2.4结构体指针与函数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B"><span class="nav-text">四、枚举类型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B"><span class="nav-text">1.定义枚举类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E5%8F%98%E9%87%8F"><span class="nav-text">2.定义枚举变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%9E%9A%E4%B8%BE%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">3.枚举变量的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%BC%95%E7%94%A8%E6%9E%9A%E4%B8%BE%E6%88%90%E5%91%98"><span class="nav-text">3.1引用枚举成员</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E6%9E%9A%E4%B8%BE%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC"><span class="nav-text">3.2枚举变量赋值</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E5%85%B1%E7%94%A8%E4%BD%93"><span class="nav-text">五、共用体</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AE%9A%E4%B9%89%E5%85%B1%E7%94%A8%E4%BD%93"><span class="nav-text">1.定义共用体</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%85%B1%E7%94%A8%E4%BD%93%E7%9A%84%E9%95%BF%E5%BA%A6"><span class="nav-text">2.共用体的长度</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%9C%A8%E5%86%85%E5%AD%98%E4%B8%AD%E7%9A%84%E5%AD%98%E5%82%A8"><span class="nav-text">3.在内存中的存储</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%B5%8B%E5%80%BC%EF%BC%9F"><span class="nav-text">4.赋值？</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E4%BD%8D%E5%9F%9F"><span class="nav-text">六、位域</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E4%BD%8D%E5%9F%9F"><span class="nav-text">1.什么是位域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%9A%E4%B9%89%E4%BD%8D%E5%9F%9F"><span class="nav-text">2.定义位域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AE%9A%E4%B9%89%E4%BD%8D%E5%9F%9F%E5%8F%98%E9%87%8F"><span class="nav-text">3.定义位域变量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%BD%8D%E5%9F%9F%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">4.位域变量的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E4%BD%8D%E5%9F%9F%E7%9A%84%E9%95%BF%E5%BA%A6"><span class="nav-text">5.位域的长度</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1%E9%95%BF%E5%BA%A6%E8%AE%A1%E7%AE%97"><span class="nav-text">5.1长度计算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%9C%A8%E5%86%85%E5%AD%98%E4%B8%AD%E7%9A%84%E5%AD%98%E5%82%A8"><span class="nav-text">6.在内存中的存储</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1%E9%9D%9E%E6%B3%95%E6%93%8D%E4%BD%9C%EF%BC%9F"><span class="nav-text">6.1非法操作？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2%E5%AD%98%E5%82%A8%E8%A7%84%E5%88%99"><span class="nav-text">6.2存储规则</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-%E6%8C%89%E4%BD%8D%E8%B5%8B%E5%80%BC"><span class="nav-text">7.按位赋值</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81-typedef"><span class="nav-text">七、  typedef  </span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF-typedef"><span class="nav-text">1.什么是  typedef  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%80%8E%E4%B9%88%E4%BD%BF%E7%94%A8%EF%BC%9F"><span class="nav-text">2.怎么使用？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%B8%B8%E8%A7%81%E7%B1%BB%E5%9E%8B%E8%B5%B7%E5%88%AB%E5%90%8D"><span class="nav-text">2.1常见类型起别名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E7%BB%93%E6%9E%84%E4%BD%93%E7%B1%BB%E5%9E%8B%E8%B5%B7%E5%88%AB%E5%90%8D"><span class="nav-text">2.2结构体类型起别名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E6%95%B0%E7%BB%84%E7%B1%BB%E5%9E%8B%E8%B5%B7%E5%88%AB%E5%90%8D"><span class="nav-text">2.3数组类型起别名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88%E7%B1%BB%E5%9E%8B%E8%B5%B7%E5%88%AB%E5%90%8D"><span class="nav-text">2.4数组指针类型起别名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E8%B5%B7%E5%88%AB%E5%90%8D"><span class="nav-text">2.5函数指针起别名</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/f72c7684.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-15-C语言-自定义数据类型 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-15-C语言-自定义数据类型
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-04 15:08:36" itemprop="dateCreated datePublished" datetime="2022-03-04T15:08:36+08:00">2022-03-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV01-C语言基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>17k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>1:03</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是C语言基础——自定义数据类型相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-15-C%E8%AF%AD%E8%A8%80-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、结构体"><a href="#一、结构体" class="headerlink" title="一、结构体"></a><font size=3>一、结构体</font></h1><p>我们常见的可以存储很多数据的结构就是数组了，但是它有一个缺点，就是只能存放相同的数据类型。</p>
<p>但在实际的问题处理过程中，有许多信息是由多个不同类型的数据组合在一起进行描述的，这些不同类型的数据是互相联系而又组成了一个有机的整体。</p>
<p>例如对于学生信息登记，整体就是整个学校或者整个班级的学生，那每个学生又有姓名，性别，年龄，学号，成绩等不同的属性。姓名为字符串，性别为字符串，年龄为整数，学号为字符串，成绩却又是浮点数，因为数据类型不同，显然不能用一个数组来存放。</p>
<p>这个时候就需要用到一种新的构造类型数据——结构体。</p>
<h2 id="1-定义结构体"><a href="#1-定义结构体" class="headerlink" title="1.定义结构体"></a><font size=3>1.定义结构体</font></h2><h3 id="1-1一般定义格式"><a href="#1-1一般定义格式" class="headerlink" title="1.1一般定义格式"></a><font size=3>1.1一般定义格式</font></h3><p>定义一个结构体数据类型，我们需要用到  struct  关键字：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">structure_name</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    data_type member_name1;</span><br><span class="line">    data_type member_name2;</span><br><span class="line">    ...</span><br><span class="line">    data_type member_nameN;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看各部分说明 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=70px>struct</td>        <td align="left">结构体定义的关键字，表明这是一个结构体的定义</td>    </tr>    <tr>        <td align="center" width=70px>structure_name</td>        <td align="left">结构体的名称，与普通变量名要求一样</td>    </tr>    <tr>        <td align="center" width=70px>data_type</td>        <td align="left">结构体内成员的数据类型，可以是基本变量类型，指针类型，枚举类型，联合体类型或者结构体类型等。比如，char，int等</td>    </tr>    <tr>        <td align="center" width=70px>member_nameN</td>        <td align="left">结构体内成员的名称（成员变量名），命名规则与变量相同</td>    </tr></table>
              </div>
            </details>

<p><strong>【注意】</strong></p>
<p>（1）结构体定义结束的时候要有分号(  ;  )。</p>
<p>（2）  { }  内的部分也称为成员列表，或者叫域表。</p>
<p>（3）结构体成员的定义方式与变量和数组的定义方式相同，只是<strong>不能初始化</strong>。</p>
<p>（4）<strong>结构体类型中的成员名可以与程序中的变量名相同</strong>，二者并不代表同一对象，编译程序可以自动对它们进行区分。 </p>
<p>（5）它属于  C  语言的一种<strong>数据类型</strong>，与整型、实型相当。因此，<strong>定义结构体时不分配空间，只有用正结构体定义变量时才分配空间</strong>。</p>
<p>（6）结构体成员中不允许存在静态变量，即不可以出现  static  。</p>
<h3 id="1-2定义实例"><a href="#1-2定义实例" class="headerlink" title="1.2定义实例"></a><font size=3>1.2定义实例</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* 注意 ; 不可缺少 */</span></span><br></pre></td></tr></table></figure>

<p>  Student  为结构体名，它包含了   5   个成员，分别是   name  、  gender  、  age  、  id  、  score  。</p>
<h2 id="2-定义结构体变量"><a href="#2-定义结构体变量" class="headerlink" title="2.定义结构体变量"></a><font size=3>2.定义结构体变量</font></h2><p>结构体是一种数据类型，在定义完结构体数据类型之后，我们就可以使用该结构体数据类型来定义变量，这样定义的变量就称之为结构体变量。结构体变量定义一般格式如下：</p>
<div class="tabs" id="tabname1"><ul class="nav-tabs"><li class="tab active"><a href="#tabname1-1">方式一</a></li><li class="tab"><a href="#tabname1-2">方式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname1-1"><ul>
<li>先定义结构体数据类型，再定义结构体变量</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu1</span>, <span class="title">stu2</span>;</span></span><br></pre></td></tr></table></figure>

<p>上边定义了两个变量   stu1   和   stu2  ，它们都是   Student   结构体类型，都由   5   个成员组成。</p>
<p><strong>【注意】</strong></p>
<p>（1）关键字  struct  不可以省略，若没有这个关键字，则系统不认为  Student   是结构体类型。</p>
<p>（2） Student  就像一个“<strong>模板</strong>”，定义出来的变量都<strong>具有相同的性质</strong>。</p>
<p>（3）程序中使用结构体变量的时候，一般不能把它作为一个整体参与数据的处理，能够参与各种运算符和操作的是结构体变量的各个成员项的数据。</p></div><div class="tab-pane" id="tabname1-2"><ul>
<li>定义结构体数据类型的同时定义结构体变量</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型同时定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125; stu1, stu2;        <span class="comment">/* ; 不可缺少 */</span></span><br></pre></td></tr></table></figure>

<p>与方式一类似，定义了两个变量   stu1   和   stu2  ，它们都是   Student   结构体类型，都由   5   个成员组成。</p>
<p><strong>【说明】</strong>如果只需要   stu1  、  stu2   两个变量，<strong>后面不需要再使用结构体名定义其他变量，那么在定义时也可以不给出结构体名</strong>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型同时定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span>               /* 省略结构体名称 */</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125; stu1, stu2;        <span class="comment">/* ; 不可缺少 */</span></span><br></pre></td></tr></table></figure></div></div></div>

<h2 id="3-使用结构体变量"><a href="#3-使用结构体变量" class="headerlink" title="3.使用结构体变量"></a><font size=3>3.使用结构体变量</font></h2><p>定义完了结构体变量，接下来就是对结构体变量的使用了，包括了赋值和引用两种操作。</p>
<h3 id="3-1引用和赋值的一般格式"><a href="#3-1引用和赋值的一般格式" class="headerlink" title="3.1引用和赋值的一般格式"></a><font size=3>3.1引用和赋值的一般格式</font></h3><p>结构体变量是一种变量，那么我们自然可以引用变量中的成员，也可以对结构体变量成员进行赋值，这些操作都是通过成员运算符    .    完成的。</p>
<ul>
<li>引用结构体变量成员的方式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">结构体变量名.成员名<span class="number">1</span></span><br><span class="line">结构体变量名.成员名<span class="number">2</span></span><br><span class="line">...</span><br><span class="line">结构体变量名.成员名N</span><br></pre></td></tr></table></figure>

<ul>
<li>对结构体变量成员赋值的方式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">结构体变量名.成员名<span class="number">1</span> = value1;</span><br><span class="line">结构体变量名.成员名<span class="number">2</span> = value2;</span><br><span class="line">...</span><br><span class="line">结构体变量名.成员名N = valueN;</span><br></pre></td></tr></table></figure>

<p><strong>【注意】</strong></p>
<p>（1）不能将结构体变量当做一个整体进行引用，只能对结构体变量的各个成员分别引用。</p>
<p>（2）在引用和赋值的过程中，要注意成员的数据类型，按我自己的理解就是当我们把结构体成员取出来的时候，取出来的这个成员就可以按照普通的变量进行赋值和引用，如果是成员是数组，那么赋值和引用就和普通数组一样。</p>
<p>（3）关于结构体变量的引用就上边的一种格式了，但是对于结构体变量的赋值，却有多种不同的方式。</p>
<p>（4）结构体变量虽然不可以整体引用，但是<strong>相同类型的结构体变量之间允许相互赋值</strong>，而<strong>不同类型的结构体变量之间不允许相互赋值</strong>，即使两者包含有同样的成员。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125; stu1 = &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.8</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu2</span>;</span></span><br><span class="line">	stu2 = stu1;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.name = %s\n&quot;</span>, stu1.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.gender = %c\n&quot;</span>, stu1.gender);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.age = %d\n&quot;</span>, stu1.age);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.id = %s\n&quot;</span>, stu1.id);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.score = %.2f\n\n&quot;</span>, stu1.score);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu2.name = %s\n&quot;</span>, stu2.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu2.gender = %c\n&quot;</span>, stu2.gender);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu2.age = %d\n&quot;</span>, stu2.age);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu2.id = %s\n&quot;</span>, stu2.id);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu2.score = %.2f\n&quot;</span>, stu2.score);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">stu1.name = qidaink</span><br><span class="line">stu1.gender = m</span><br><span class="line">stu1.age = 18</span><br><span class="line">stu1.id = 0000000001</span><br><span class="line">stu1.score = 95.80</span><br><span class="line"></span><br><span class="line">stu2.name = qidaink</span><br><span class="line">stu2.gender = m</span><br><span class="line">stu2.age = 18</span><br><span class="line">stu2.id = 0000000001</span><br><span class="line">stu2.score = 95.80</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2结构体变量赋值"><a href="#3-2结构体变量赋值" class="headerlink" title="3.2结构体变量赋值"></a><font size=3>3.2结构体变量赋值</font></h3><p>关于结构体变量的这几种赋值方式，我们都通过实例来进行说明。</p>
<h4 id="3-2-1赋值方式一"><a href="#3-2-1赋值方式一" class="headerlink" title="3.2.1赋值方式一"></a><font size=3>3.2.1赋值方式一</font></h4><p>我们可以先定义结构体变量，然后再对各个成员进行赋值。</p>
<ul>
<li>一般使用格式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 对结构体成员逐个进行赋值 */</span></span><br><span class="line">stu1.name = <span class="string">&quot;qidaink&quot;</span>;</span><br><span class="line">stu1.gender = <span class="string">&#x27;m&#x27;</span>;</span><br><span class="line">stu1.age = <span class="number">18</span>;</span><br><span class="line"><span class="built_in">strcpy</span>(stu1.id, <span class="string">&quot;0000000001&quot;</span>);</span><br><span class="line">stu1.score = <span class="number">95.8</span>;</span><br></pre></td></tr></table></figure>

<ul>
<li>使用实例</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看赋值实例源码 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.定义结构体变量 */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span>;</span></span><br><span class="line">	<span class="comment">/* 2.对结构体成员逐个进行赋值 */</span></span><br><span class="line">	stu1.name = <span class="string">&quot;qidaink&quot;</span>;</span><br><span class="line">	stu1.gender = <span class="string">&#x27;m&#x27;</span>;</span><br><span class="line">	stu1.age = <span class="number">18</span>;</span><br><span class="line">	<span class="built_in">strcpy</span>(stu1.id, <span class="string">&quot;0000000001&quot;</span>);</span><br><span class="line">	stu1.score = <span class="number">95.8</span>;</span><br><span class="line">	<span class="comment">/* 3.打印各个成员的数据 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.name = %s\n&quot;</span>, stu1.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.gender = %c\n&quot;</span>, stu1.gender);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.age = %d\n&quot;</span>, stu1.age);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.id = %s\n&quot;</span>, stu1.id);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.score = %.2f\n&quot;</span>, stu1.score);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">stu1.name = qidaink</span><br><span class="line">stu1.gender = m</span><br><span class="line">stu1.age = 18</span><br><span class="line">stu1.id = 0000000001</span><br><span class="line">stu1.score = 95.80</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h4 id="3-2-2赋值方式二"><a href="#3-2-2赋值方式二" class="headerlink" title="3.2.2赋值方式二"></a><font size=3>3.2.2赋值方式二</font></h4><p>我们可以在定义结构体变量的同时进行结构体成员的赋值。</p>
<ul>
<li>一般使用格式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br></pre></td></tr></table></figure>

<ul>
<li>使用实例</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看赋值实例源码 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.定义结构体变量 */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line">	<span class="comment">/* 2.打印各个成员的数据 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.name = %s\n&quot;</span>, stu1.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.gender = %c\n&quot;</span>, stu1.gender);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.age = %d\n&quot;</span>, stu1.age);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.id = %s\n&quot;</span>, stu1.id);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.score = %.2f\n&quot;</span>, stu1.score);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">stu1.name = qidaink</span><br><span class="line">stu1.gender = m</span><br><span class="line">stu1.age = 18</span><br><span class="line">stu1.id = 0000000001</span><br><span class="line">stu1.score = 95.80</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h4 id="3-2-3赋值方式三"><a href="#3-2-3赋值方式三" class="headerlink" title="3.2.3赋值方式三"></a><font size=3>3.2.3赋值方式三</font></h4><p>我们可以在定义结构体变量的同时进行结构体成员的<strong>乱序赋值</strong>。</p>
<div class="tabs" id="tabname2"><ul class="nav-tabs"><li class="tab active"><a href="#tabname2-1">C风格</a></li><li class="tab"><a href="#tabname2-2">C++风格</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname2-1"><ul>
<li>一般使用格式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span></span><br><span class="line">&#123;</span><br><span class="line">	.id = <span class="string">&quot;0000000001&quot;</span>,</span><br><span class="line">	.gender = <span class="string">&#x27;m&#x27;</span>,</span><br><span class="line">	.name = <span class="string">&quot;qidaink&quot;</span>,</span><br><span class="line">	.age = <span class="number">18</span>,</span><br><span class="line">	.score = <span class="number">95.8</span></span><br><span class="line">&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br></pre></td></tr></table></figure>

<ul>
<li>使用实例</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看赋值实例源码 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.定义结构体变量 */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span></span><br><span class="line">	&#123;</span><br><span class="line">		.id = <span class="string">&quot;0000000001&quot;</span>,</span><br><span class="line">		.gender = <span class="string">&#x27;m&#x27;</span>,</span><br><span class="line">		.name = <span class="string">&quot;qidaink&quot;</span>,</span><br><span class="line">		.age = <span class="number">18</span>,</span><br><span class="line">		.score = <span class="number">95.8</span></span><br><span class="line">	&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line">	<span class="comment">/* 2.打印各个成员的数据 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.name = %s\n&quot;</span>, stu1.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.gender = %c\n&quot;</span>, stu1.gender);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.age = %d\n&quot;</span>, stu1.age);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.id = %s\n&quot;</span>, stu1.id);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.score = %.2f\n&quot;</span>, stu1.score);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">stu1.name = qidaink</span><br><span class="line">stu1.gender = m</span><br><span class="line">stu1.age = 18</span><br><span class="line">stu1.id = 0000000001</span><br><span class="line">stu1.score = 95.80</span><br></pre></td></tr></table></figure>
              </div>
            </details></div><div class="tab-pane" id="tabname2-2"><ul>
<li>一般使用格式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span></span><br><span class="line">&#123;</span><br><span class="line">	id : <span class="string">&quot;0000000001&quot;</span>,</span><br><span class="line">	gender : <span class="string">&#x27;m&#x27;</span>,</span><br><span class="line">	name : <span class="string">&quot;qidaink&quot;</span>,</span><br><span class="line">	age : <span class="number">18</span>,</span><br><span class="line">	score : <span class="number">95.8</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<ul>
<li>使用实例</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看赋值实例源码 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                    <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 1.定义结构体变量 */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span></span><br><span class="line">	&#123;</span><br><span class="line">		id : <span class="string">&quot;0000000001&quot;</span>,</span><br><span class="line">		gender : <span class="string">&#x27;m&#x27;</span>,</span><br><span class="line">		name : <span class="string">&quot;qidaink&quot;</span>,</span><br><span class="line">		age : <span class="number">18</span>,</span><br><span class="line">		score : <span class="number">95.8</span></span><br><span class="line">	&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line">	<span class="comment">/* 2.打印各个成员的数据 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.name = %s\n&quot;</span>, stu1.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.gender = %c\n&quot;</span>, stu1.gender);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.age = %d\n&quot;</span>, stu1.age);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.id = %s\n&quot;</span>, stu1.id);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.score = %.2f\n&quot;</span>, stu1.score);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">stu1.name = qidaink</span><br><span class="line">stu1.gender = m</span><br><span class="line">stu1.age = 18</span><br><span class="line">stu1.id = 0000000001</span><br><span class="line">stu1.score = 95.80</span><br></pre></td></tr></table></figure>
              </div>
            </details></div></div></div>

<h4 id="3-2-4赋值方式四"><a href="#3-2-4赋值方式四" class="headerlink" title="3.2.4赋值方式四"></a><font size=3>3.2.4赋值方式四</font></h4><p>我们可以在定义结构体定义结构体时直接定义结构体变量，并直接对结构体变量赋值。</p>
<ul>
<li>一般使用格式</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125; stu1 = &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* ; 不可缺少 */</span></span><br></pre></td></tr></table></figure>

<ul>
<li>使用实例</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看赋值实例源码 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span>  *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span>  gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span>   age;        <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span>  id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;      <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125; stu1 = &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 打印各个成员的数据 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.name = %s\n&quot;</span>, stu1.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.gender = %c\n&quot;</span>, stu1.gender);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.age = %d\n&quot;</span>, stu1.age);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.id = %s\n&quot;</span>, stu1.id);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.score = %.2f\n&quot;</span>, stu1.score);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">stu1.name = qidaink</span><br><span class="line">stu1.gender = m</span><br><span class="line">stu1.age = 18</span><br><span class="line">stu1.id = 0000000001</span><br><span class="line">stu1.score = 95.80</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3结构体变量嵌套赋值"><a href="#3-3结构体变量嵌套赋值" class="headerlink" title="3.3结构体变量嵌套赋值"></a><font size=3>3.3结构体变量嵌套赋值</font></h3><p>如果说成员本身又属于一个结构体类型，那么我们就需要用若干个成员运算符，一级一级地找到最低一级的成员。只能对最低级的成员进行赋值或存取以及计算。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">	&#123;</span></span><br><span class="line">		<span class="type">int</span> year;</span><br><span class="line">		<span class="type">int</span> month;</span><br><span class="line">		<span class="type">int</span> day;</span><br><span class="line">	&#125; birthday;</span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span>;</span> <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"></span><br><span class="line">	stu1.name = <span class="string">&quot;qidaink&quot;</span>;</span><br><span class="line"></span><br><span class="line">	stu1.birthday.year = <span class="number">2000</span>;</span><br><span class="line">	stu1.birthday.month = <span class="number">7</span>;</span><br><span class="line">	stu1.birthday.day = <span class="number">7</span>;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.name = %s\n&quot;</span>, stu1.name);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.birthday.year = %d\n&quot;</span>,  stu1.birthday.year);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.birthday.month = %d\n&quot;</span>,  stu1.birthday.month);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;stu1.birthday.day = %d\n&quot;</span>,  stu1.birthday.day);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">stu1.name = qidaink</span><br><span class="line">stu1.birthday.year = 2000</span><br><span class="line">stu1.birthday.month = 7</span><br><span class="line">stu1.birthday.day = 7</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-结构体的长度"><a href="#4-结构体的长度" class="headerlink" title="4.结构体的长度"></a><font size=3>4.结构体的长度</font></h2><h3 id="4-1计算格式"><a href="#4-1计算格式" class="headerlink" title="4.1计算格式"></a><font size=3>4.1计算格式</font></h3><p>当我们定义了一个结构体后，用它定义出来的结构体变量占多大空间呢？我们可以使用  sizeof  函数来求得所定义结构体的长度。一般格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">sizeof</span>( <span class="keyword">struct</span> structure_name) </span><br><span class="line"><span class="comment">/* 或者使用结构体变量来求 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">structure_name</span> <span class="title">structure_variable</span>;</span></span><br><span class="line"><span class="keyword">sizeof</span>(structure_variable) </span><br></pre></td></tr></table></figure>

<h3 id="4-2使用实例"><a href="#4-2使用实例" class="headerlink" title="4.2使用实例"></a><font size=3>4.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">3</span>];      <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;01&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof( struct Student) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> STU));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.name) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.name));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.gender) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.gender));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.age) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.age));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.id) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.id));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.score) = %.ld\n\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.score));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">sizeof(stu1) = 24</span><br><span class="line">sizeof( struct Student) = 24</span><br><span class="line">sizeof(stu1.name) = 8</span><br><span class="line">sizeof(stu1.gender) = 1</span><br><span class="line">sizeof(stu1.age) = 4</span><br><span class="line">sizeof(stu1.id) = 3</span><br><span class="line">sizeof(stu1.score) = 4</span><br></pre></td></tr></table></figure><p><strong>【说明</strong>】  64  位平台下的指针变量固定为  8  字节。</p><p>我们会发现，总的大小并非是各个成员所占空间大小的总和，那我们就接着往后看。</p>
              </div>
            </details>

<h2 id="5-在内存中的存储"><a href="#5-在内存中的存储" class="headerlink" title="5.在内存中的存储"></a><font size=3>5.在内存中的存储</font></h2><p>当结构体定义了一个结构体变量的时候，这个结构体变量在内存中是怎样的呢？</p>
<details class="folding-tag" blue><summary> 点击查看测试实例 </summary>
              <div class='content'>
              <p>【说明】结构体在定义的时候并不分配内存空间，所以无法对结构体名称进行取地址操作，但是当用它定义了结构体变量之后，就可以使用  &amp;  符号来获取结构体变量在内存中的起始地址了。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">3</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu1</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;01&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof( struct STU) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> STU));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1        = %p,sizeof(stu1) = %ld\n&quot;</span>, &amp;stu1, <span class="keyword">sizeof</span>(stu1));</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.name   = %p,sizeof(stu1.name) = %ld\n&quot;</span>, &amp;stu1.name, <span class="keyword">sizeof</span>(stu1.name));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.gender = %p,sizeof(stu1.gender) = %ld\n&quot;</span>, &amp;stu1.gender, <span class="keyword">sizeof</span>(stu1.gender));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.age    = %p,sizeof(stu1.age) = %ld\n&quot;</span>, &amp;stu1.age, <span class="keyword">sizeof</span>(stu1.age));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.id     = %p,sizeof(stu1.id) = %ld\n&quot;</span>, &amp;stu1.id, <span class="keyword">sizeof</span>(stu1.id));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.id[0]  = %p,sizeof(stu1.id[0]) = %ld\n&quot;</span>, &amp;stu1.id[<span class="number">0</span>], <span class="keyword">sizeof</span>(stu1.id[<span class="number">0</span>]));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.id[1]  = %p,sizeof(stu1.id[1]) = %ld\n&quot;</span>, &amp;stu1.id[<span class="number">1</span>], <span class="keyword">sizeof</span>(stu1.id[<span class="number">1</span>]));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.id[2]  = %p,sizeof(stu1.id[2]) = %ld\n&quot;</span>, &amp;stu1.id[<span class="number">2</span>], <span class="keyword">sizeof</span>(stu1.id[<span class="number">2</span>]));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;&amp;stu1.score  = %p,sizeof(stu1.score) = %.ld\n\n&quot;</span>, &amp;stu1.score, <span class="keyword">sizeof</span>(stu1.score));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">sizeof( struct Student) = 24</span><br><span class="line">&amp;stu1        = 0x7fff1635a610,sizeof(stu1) = 24</span><br><span class="line">&amp;stu1.name   = 0x7fff1635a610,sizeof(stu1.name) = 8</span><br><span class="line">&amp;stu1.gender = 0x7fff1635a618,sizeof(stu1.gender) = 1</span><br><span class="line">&amp;stu1.age    = 0x7fff1635a61c,sizeof(stu1.age) = 4</span><br><span class="line">&amp;stu1.id     = 0x7fff1635a620,sizeof(stu1.id) = 3</span><br><span class="line">&amp;stu1.id[0]  = 0x7fff1635a620,sizeof(stu1.id[0]) = 1</span><br><span class="line">&amp;stu1.id[1]  = 0x7fff1635a621,sizeof(stu1.id[1]) = 1</span><br><span class="line">&amp;stu1.id[2]  = 0x7fff1635a622,sizeof(stu1.id[2]) = 1</span><br><span class="line">&amp;stu1.score  = 0x7fff1635a624,sizeof(stu1.score) = 4</span><br></pre></td></tr></table></figure><p><strong>【说明】</strong>  64  位平台下的指针变量固定为  8  字节。</p>
              </div>
            </details>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-15-C%E8%AF%AD%E8%A8%80-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/img/image-20220326102515821.png" alt="image-20220326102515821" style="zoom:50%;" />

<p>从最终例子中打印的数据分析，  gender  是一个  char  类型的数据，只占用了  1  个字节的数据，但是它后边空出来了  3  个字节数据。数组  id  也是，总长度是  3  字节，”空”出了  1  个字节空间。这样算下来，总长度就是  8+4+4+8&#x3D;24  了，这也就对应上了。但是，这些”空”出来的空间是为什么呢？这就涉及到了字节对齐，这个请看下一节。</p>
<h2 id="6-字节对齐"><a href="#6-字节对齐" class="headerlink" title="6.字节对齐"></a><font size=3>6.字节对齐</font></h2><h3 id="6-1字节对齐是什么？"><a href="#6-1字节对齐是什么？" class="headerlink" title="6.1字节对齐是什么？"></a><font size=3>6.1字节对齐是什么？</font></h3><p>上一节,出现了一些”空”的地址空间，所谓”空”其实也不是里面真的什么都没有，它就同定义了一个变量但没有初始化一样，里面是一个很小的、负的填充字。为了便于表达，我们就暂且称之为”空”好了。</p>
<p>字节对齐也可以叫内存对齐，计算机中内存大小的基本单位是字节（  byte  ），<strong>理论上</strong>来讲，可以从<strong>任意地址访问</strong>某种基本数据类型，但是<strong>实际上</strong>，计算机<strong>并非逐字节大小读写内存</strong>，而是以  2  ,  4  ,或  8  的倍数的字节块来读写内存，如此一来就会对基本数据类型的合法地址作出一些限制，即它的地址必须是  2  ，  4  或  8  的倍数。</p>
<p>这种对齐限制简化了形成处理器和存储器系统之间的接口的硬件设计。对齐跟数据在内存中的位置有关。如果一个变量的内存地址正好位于它长度的整数倍，他就被称做<strong>自然对齐</strong>。例如在  32  位  CPU  下，假设一个整型变量的地址为  0x00000004  ，那它就是自然对齐的。再比如存放  int  型数据的地址一定要是  4  的倍数，存放  short  型数据的地址一定要是  2  的倍数。</p>
<h3 id="6-2为什么要字节对齐？"><a href="#6-2为什么要字节对齐？" class="headerlink" title="6.2为什么要字节对齐？"></a><font size=3>6.2为什么要字节对齐？</font></h3><p>根本在于为了让  CPU  更有效率地访问内存空间。例如，假设一个处理器总是从存储器中取出  8  个字节，则地址必须为  8  的倍数。如果我们能保证将所有的  double  类型数据的地址对齐成  8  的倍数，那么就可以用一个存储器操作来读或者写值了。如果对象被分放在两个  8  字节存储块中，横跨两个区域，我们就需要执行两次存储器访问操作。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <p>假设一个整型变量的地址不是自然对齐，比如为  0x00000002  ，则  CPU  如果取它的值的话需要访问两次内存，第一次取从  0x00000002-0x00000003  的一个  short  长度的数据，第二次取从  0x00000004-0x00000005  的一个  short  长度的数据，然后组合得到所要的数据；</p><p>如果变量在  0x00000003  地址上的话则要访问三次内存，第一次为  char  长度，第二次为  short  长度，第三次为  char  长度，然后组合得到整型数据。</p><p>如果变量在自然对齐位置上，则只要一次就可以取出数据。</p>
              </div>
            </details>

<h3 id="6-3对齐原则？"><a href="#6-3对齐原则？" class="headerlink" title="6.3对齐原则？"></a><font size=3>6.3对齐原则？</font></h3><details class="folding-tag" blue><summary> 点击查看 #pragma pack 说明 </summary>
              <div class='content'>
              <p>这是一个预处理指令。</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack <span class="comment">/* 主要用作改变编译器的默认对齐方式。 */</span></span></span><br></pre></td></tr></table></figure><ul><li>基本使用方法</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack(n)      <span class="comment">/* 设置编辑器按照n个字节对齐，n可以取值1,2,4,8,16 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack()       <span class="comment">/* 取消自定义字节对齐方式。 */</span></span></span><br></pre></td></tr></table></figure><p>还有其他的用法，暂时用到的不过，后边有机会再专门总结一下吧。</p><details class="folding-tag" blue><summary> 点击查看实例 </summary>              <div class='content'>              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack(1) <span class="comment">/* 字节对齐改成了4个字节 */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">3</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> pack() <span class="comment">/* 取消自定义字节对齐 */</span></span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Test</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">3</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu1</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;01&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof( struct Student) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.name) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.name));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.gender) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.gender));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.age) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.age));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.id) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.id));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu1.score) = %.ld\n\n&quot;</span>, <span class="keyword">sizeof</span>(stu1.score));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">Test</span> <span class="title">stu2</span> =</span> &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;01&quot;</span>, <span class="number">95.8</span>&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof( struct Student) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.name) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.name));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.gender) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.gender));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.age) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.age));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.id) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.id));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;sizeof(stu2.score) = %.ld\n\n&quot;</span>, <span class="keyword">sizeof</span>(stu2.score));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译链接程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">sizeof(stu1) = 20</span><br><span class="line">sizeof( struct Student) = 20</span><br><span class="line">sizeof(stu1.name) = 8</span><br><span class="line">sizeof(stu1.gender) = 1</span><br><span class="line">sizeof(stu1.age) = 4</span><br><span class="line">sizeof(stu1.id) = 3</span><br><span class="line">sizeof(stu1.score) = 4</span><br><span class="line"></span><br><span class="line">sizeof(stu2) = 24</span><br><span class="line">sizeof( struct Student) = 20</span><br><span class="line">sizeof(stu2.name) = 8</span><br><span class="line">sizeof(stu2.gender) = 1</span><br><span class="line">sizeof(stu2.age) = 4</span><br><span class="line">sizeof(stu2.id) = 3</span><br><span class="line">sizeof(stu2.score) = 4</span><br></pre></td></tr></table></figure><p><strong>【说明】</strong>  64  位平台下的指针变量固定为  8  字节。</p><p>我们会发现，相同的结构体属性，但是对齐方式不同，占用的空间也就不一样了。</p>              </div>            </details>
              </div>
            </details>

<p><strong>【原则1】</strong>数据成员对齐规则：结构(  struct  )或联合(  union  )的数据成员，第一个数据成员放在  offset  为  0  的地方，以后每个数据成员的对齐按照  #pragma pack  指定的数值和这个数据成员自身长度中，比较小的那个进行。</p>
<p><strong>【原则2】</strong>结构或联合的整体对齐规则：在数据成员完成各自对齐之后，结构或联合本身也要进行对齐，对齐将按照  #pragma pack  指定的数值和结构(或联合)最大数据成员长度中，比较小的那个进行。</p>
<p><strong>【原则3】</strong>结构体作为成员：如果一个结构里有某些结构体成员，则结构体成员要从其内部最大元素大小的整数倍地址开始存储。</p>
<p>备注：数组成员按长度按数组类型长度计算，如char t[9],在第1步中数据自身长度按1算，累加结构体时长度为9；第2步中，找最大数据长度时，如果结构体T有复杂类型成员A，该A成员的长度为该复杂类型成员A的最大成员长度。</p>
<p><strong>【注意】</strong></p>
<p>（1）当  #pragma pack  的  n  值等于或超过所有数据成员长度的时候，这个  n  值的大小将不产生任何效果。</p>
<p>（2）对齐位数跟处理器位数和编译器都有关。  VS  ,   VC  等编译器默认是  #pragma pack(8)  ，所以测试我们的规则会正常；注意  gcc  默认是  #pragma pack(4)  ，并且  gcc  只支持  1  ,  2  ,  4  对齐。套用三原则里计算的对齐值是不能大于  #pragma pack  指定的  n  值。</p>
<p>这样上边结构体的成员分布也就很清楚明白了。</p>
<h1 id="二、结构体数组"><a href="#二、结构体数组" class="headerlink" title="二、结构体数组"></a><font size=3>二、结构体数组</font></h1><p>具有相同结构体类型的结构体变量也可以组成数组，称它们为<strong>结构体数组</strong>。结构体数组的<strong>每一个数组元素都是结构体类型的数据</strong>，它们都分别包括各个成员（分量）项。</p>
<h2 id="1-定义结构体数组"><a href="#1-定义结构体数组" class="headerlink" title="1.定义结构体数组"></a><font size=3>1.定义结构体数组</font></h2><p>定义结构体数组的方式与定义结构体变量的方式类似。</p>
<div class="tabs" id="tabname3"><ul class="nav-tabs"><li class="tab active"><a href="#tabname3-1">方式一</a></li><li class="tab"><a href="#tabname3-2">方式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname3-1"><ul>
<li>先定义结构体数据类型，再定义结构体数组</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数组 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span> <span class="title">stu</span>[6];</span></span><br></pre></td></tr></table></figure>

<p>定义了一个结构体数组  stu  ，它有  6  个元素，每个元素都是   Student   结构体类型，每个元素都由   5   个成员组成。</p></div><div class="tab-pane" id="tabname3-2"><ul>
<li>定义结构体数据类型的同时定义结构体数组</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型同时定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">STU</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">	<span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">	<span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">	<span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">	<span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125; stu1[<span class="number">6</span>];           <span class="comment">/* ; 不可缺少 */</span></span><br></pre></td></tr></table></figure>

<p>与方式一类似，定义了一个结构体数组  stu  ，它有  6  个元素，每个元素都是   Student   结构体类型，每个元素都由   5   个成员组成。</p>
<p><strong>【说明】</strong>如果只需要   stu   一个结构体数组，<strong>后面不需要再使用结构体名定义其他变量，那么在定义时也可以不给出结构体名</strong>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型同时定义结构体变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span>               /* 省略结构体名称 */</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;stu[<span class="number">6</span>];             <span class="comment">/* ; 不可缺少 */</span></span><br></pre></td></tr></table></figure></div></div></div>

<h2 id="2-结构体数组的使用"><a href="#2-结构体数组的使用" class="headerlink" title="2.结构体数组的使用"></a><font size=3>2.结构体数组的使用</font></h2><p>其实这与变量的赋值也是很类似的。</p>
<h3 id="2-1使用格式"><a href="#2-1使用格式" class="headerlink" title="2.1使用格式"></a><font size=3>2.1使用格式</font></h3><ul>
<li>引用结构体变量成员的方式如下：</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">结构体数组名[index].成员名<span class="number">1</span></span><br><span class="line">结构体数组名[index].成员名<span class="number">2</span></span><br><span class="line">...</span><br><span class="line">结构体数组名[index].成员名N</span><br></pre></td></tr></table></figure>

<ul>
<li>对结构体变量成员赋值的方式如下：</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">结构体数组名[index].成员名<span class="number">1</span> = value1;</span><br><span class="line">结构体数组名[index].成员名<span class="number">2</span> = value2;</span><br><span class="line">...</span><br><span class="line">结构体数组名[index].成员名N = valueN;</span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）访问或者赋值的时候注意遵循数组访问格式，当访问到某个元素后，再对元素的成员进行访问即可。多维数组也是一样。</p>
<p>（2）可以将一个结构体数组元素值赋给<strong>同一结构体类型</strong>的数组中的另一个元素，或者赋给<strong>同一类型</strong>的结构体变量。</p>
<p>（3）我们<strong>不能把结构体数组元素作为一个整体</strong>直接进行输入输出，另外与普通数组一样，需要使用循环遍历整个结构体数组。</p>
<h3 id="2-2赋值与初始化"><a href="#2-2赋值与初始化" class="headerlink" title="2.2赋值与初始化"></a><font size=3>2.2赋值与初始化</font></h3><p>这里的话直接看实例把，其实没啥说的，就跟一般的数组一样的，注意结构体数据的引用方式即可。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <p>【说明】赋值方式二和赋值方式三也可以称之为结构体数组的初始化。</p><div class="tabs" id="tabname4"><ul class="nav-tabs"><li class="tab active"><a href="#tabname4-1">赋值方式一</a></li><li class="tab"><a href="#tabname4-2">赋值方式二</a></li><li class="tab"><a href="#tabname4-3">赋值方式三</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname4-1"><ul><li>先定义结构体数组，再赋值</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu</span>[3];</span> <span class="comment">/* 定义结构体数组 */</span></span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">    stu[<span class="number">1</span>].name = <span class="string">&quot;qidaink&quot;</span>;</span><br><span class="line">    stu[<span class="number">1</span>].gender = <span class="string">&#x27;m&#x27;</span>;</span><br><span class="line">    stu[<span class="number">1</span>].age = <span class="number">18</span>;</span><br><span class="line">    <span class="built_in">strcpy</span>(stu[<span class="number">1</span>].id, <span class="string">&quot;0000000001&quot;</span>);</span><br><span class="line">    stu[<span class="number">1</span>].score = <span class="number">95.8</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="keyword">sizeof</span>(stu)/<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student); i++)</span><br><span class="line">    &#123;</span><br><span class="line">       <span class="built_in">printf</span>(<span class="string">&quot;i = %d, %s %c %d %s %.2f\n&quot;</span>, i, stu[i].name, stu[i].gender, stu[i].age, stu[i].id, stu[i].score);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">i = 0, (null)  0         0.00</span><br><span class="line">i = 1, qidaink m 18 0000000001 95.80</span><br><span class="line">i = 2, UH��AWAVAUATSH��(L��!  0 ��JTV 0.00</span><br></pre></td></tr></table></figure><p>可以看到，出现了很多乱码，这就是因为数组没有初始化造成的。</p></div><div class="tab-pane" id="tabname4-2"><ul><li>先定义结构体，然后在定义结构体数组的时候进行赋值</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 语法格式 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> 结构体名</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    数据类型 成员名<span class="number">1</span>;</span><br><span class="line">    数据类型 成员名<span class="number">2</span>;</span><br><span class="line">    ...</span><br><span class="line">    数据类型 成员名N;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> 结构体名 数组名[元素个数] =</span> &#123; 初始数据表 &#125;</span><br></pre></td></tr></table></figure><p>示例如下：</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu</span>[3] =</span> &#123;&#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.8</span>&#125;&#125;; <span class="comment">/* 定义结构体变量 */</span></span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="keyword">sizeof</span>(stu)/<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student); i++)</span><br><span class="line">    &#123;</span><br><span class="line">       <span class="built_in">printf</span>(<span class="string">&quot;i = %d, %s %c %d %s %.2f\n&quot;</span>, i, stu[i].name, stu[i].gender, stu[i].age, stu[i].id, stu[i].score);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">i = 0, qidaink m 18 0000000001 95.80</span><br><span class="line">i = 1, (null)  0  0.00</span><br><span class="line">i = 2, (null)  0  0.00</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname4-3"><ul><li>定义结构体时直接对结构体数组赋值</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 语法格式 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> 结构体名</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    数据类型 成员名<span class="number">1</span>;</span><br><span class="line">    数据类型 成员名<span class="number">2</span>;</span><br><span class="line">    ...</span><br><span class="line">    数据类型 成员名N;</span><br><span class="line">&#125;数组名[元素个数] = &#123; 初始数据表 &#125;;</span><br></pre></td></tr></table></figure><p>示例如下：</p><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;stu[<span class="number">3</span>] = &#123;&#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.8</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;  </span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="keyword">sizeof</span>(stu)/<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student); i++)</span><br><span class="line">    &#123;</span><br><span class="line">       <span class="built_in">printf</span>(<span class="string">&quot;i = %d, %s %c %d %s %.2f\n&quot;</span>, i, stu[i].name, stu[i].gender, stu[i].age, stu[i].id, stu[i].score);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">i = 0, qidaink m 18 0000000001 95.80</span><br><span class="line">i = 1, (null)  0  0.00</span><br><span class="line">i = 2, (null)  0  0.00</span><br></pre></td></tr></table></figure></div></div></div>
              </div>
            </details>

<h1 id="三、结构体指针"><a href="#三、结构体指针" class="headerlink" title="三、结构体指针"></a><font size=3>三、结构体指针</font></h1><p>我们可以设定一个指针变量用来指向一个结构体变量。此时该指针变量的值是结构体变量的起始地址，该指针称为结构体指针。</p>
<p>结构体指针<strong>与前面的各种指针变量在特性和方法上是相同的</strong>。与前述相同，在程序中结构体指针也是通过访问目标运算  *  访问它的对象。</p>
<h2 id="1-定义结构体指针"><a href="#1-定义结构体指针" class="headerlink" title="1.定义结构体指针"></a><font size=3>1.定义结构体指针</font></h2><p>结构体指针的一般声明形式为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> 结构体名 *结构体指名；</span></span><br></pre></td></tr></table></figure>

<p>其中的结构体名必须是已经定义过的结构体类型。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义结构体数组 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span> *<span class="title">pStu</span>;</span></span><br></pre></td></tr></table></figure><p>  pStu  是指向  struct Student  结构体类型的指针。结构体指针的说明规定了它的数据特性，并为结构体指针本身分配了一定的内存空间。但是指针的内容尚未确定，即<strong>它指向随机的对象</strong>。</p>
              </div>
            </details>

<h2 id="2-结构体指针的使用"><a href="#2-结构体指针的使用" class="headerlink" title="2.结构体指针的使用"></a><font size=3>2.结构体指针的使用</font></h2><h3 id="2-1使用格式-1"><a href="#2-1使用格式-1" class="headerlink" title="2.1使用格式"></a><font size=3>2.1使用格式</font></h3><p>一般使用格式有以下两种方式：</p>
<div class="tabs" id="tabname5"><ul class="nav-tabs"><li class="tab active"><a href="#tabname5-1">格式一</a></li><li class="tab"><a href="#tabname5-2">格式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname5-1"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">(*结构体指针名).成员名<span class="number">1</span></span><br><span class="line">(*结构体指针名).成员名<span class="number">2</span></span><br><span class="line">...</span><br><span class="line">(*结构体指针名).成员名N</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname5-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">结构体指针名-&gt;成员名<span class="number">1</span></span><br><span class="line">结构体指针名-&gt;成员名<span class="number">2</span></span><br><span class="line">...</span><br><span class="line">结构体指针名-&gt;成员名N</span><br></pre></td></tr></table></figure>

<p>【注意】确保是一个结构体指针的情况下才可以使用  -&gt;  来获取成员</p></div></div></div>

<h3 id="2-2赋值与初始化-1"><a href="#2-2赋值与初始化-1" class="headerlink" title="2.2赋值与初始化"></a><font size=3>2.2赋值与初始化</font></h3><p>与之前的指针变量格式一样。，赋值时要给的是一个地址。</p>
<div class="tabs" id="tabname6"><ul class="nav-tabs"><li class="tab active"><a href="#tabname6-1">赋值方式一</a></li><li class="tab"><a href="#tabname6-2">赋值方式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname6-1"><ul>
<li>先定义结构体指针，再赋值</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu_v</span> =</span> &#123;<span class="string">&quot;fanhua&quot;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000003&quot;</span>, <span class="number">97.5</span>&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> *<span class="title">pStu</span>;</span></span><br><span class="line"></span><br><span class="line">    pStu = &amp;stu_v;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s %c %d %s %.2f\n&quot;</span>, pStu-&gt;name, pStu-&gt;gender, pStu-&gt;age, pStu-&gt;id, pStu-&gt;score);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在终端执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure>

<p>会看到有如下信息输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fanhua g 18 0000000003 97.50</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname6-2"><ul>
<li>在定义结构体指针的时候直接进行赋值</li>
</ul>
<p>这种赋值方式也被称为初始化。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 语法格式 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> 结构体名</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    数据类型 成员名<span class="number">1</span>;</span><br><span class="line">    数据类型 成员名<span class="number">2</span>;</span><br><span class="line">    ...</span><br><span class="line">    数据类型 成员名N;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> 结构体名 *指针变量名 =</span> address;</span><br></pre></td></tr></table></figure>

<p>示例如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu_v</span> =</span> &#123;<span class="string">&quot;fanhua&quot;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000003&quot;</span>, <span class="number">97.5</span>&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> *<span class="title">pStu</span> =</span> &amp;stu_v;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s %c %d %s %.2f\n&quot;</span>, pStu-&gt;name, pStu-&gt;gender, pStu-&gt;age, pStu-&gt;id, pStu-&gt;score);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在终端执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure>

<p>会看到有如下信息输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fanhua g 18 0000000003 97.50</span><br></pre></td></tr></table></figure></div></div></div>

<h3 id="2-3结构体指针与数组"><a href="#2-3结构体指针与数组" class="headerlink" title="2.3结构体指针与数组"></a><font size=3>2.3结构体指针与数组</font></h3><p>结构体指针变量自然也可以指向数组，帮助我们访问数组中的元素。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu</span>[3] =</span> &#123;&#123;<span class="string">&quot;fanhua&quot;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000003&quot;</span>, <span class="number">97.5</span>&#125;,</span><br><span class="line">                             &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">19</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.67</span>&#125;,</span><br><span class="line">                             &#123;<span class="string">&quot;a&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">16</span>, <span class="string">&quot;0000000002&quot;</span>, <span class="number">87.5</span>&#125;&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> *<span class="title">pStu</span> =</span> stu;</span><br><span class="line">    <span class="type">int</span> len = <span class="keyword">sizeof</span>(stu) / <span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Name\tGender\tAge\t    ID\t\tScore\t\n&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span>(pStu = stu; pStu &lt; stu + len; pStu++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%s\t  %c\t%d\t%s\t%.2f\n&quot;</span>, pStu-&gt;name, pStu-&gt;gender, pStu-&gt;age, pStu-&gt;id, pStu-&gt;score);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Name    Gender  Age         ID          Score</span><br><span class="line">fanhua    g     18      0000000003      97.50</span><br><span class="line">qidaink   m     19      0000000001      95.67</span><br><span class="line">a         m     16      0000000002      87.50</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<p>【注意】当一个结构体指针指向数组后，它可以作为数组名使用，但是无法再像结构体指针那样可以使用  -&gt;  来取相应元素的成员，但是可以使用  .  来获取相应元素的成员。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu</span>[3] =</span> &#123;&#123;<span class="string">&quot;fanhua&quot;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000003&quot;</span>, <span class="number">97.5</span>&#125;,</span><br><span class="line">                             &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">19</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.67</span>&#125;,</span><br><span class="line">                             &#123;<span class="string">&quot;a&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">16</span>, <span class="string">&quot;0000000002&quot;</span>, <span class="number">87.5</span>&#125;&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> *<span class="title">pStu</span> =</span> stu;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s\t  %c\t%d\t%s\t%.2f\n&quot;</span>, pStu[<span class="number">0</span>]-&gt;name, pStu[<span class="number">0</span>]-&gt;gender, pStu[<span class="number">0</span>]-&gt;age, pStu[<span class="number">0</span>]-&gt;id, pStu[<span class="number">0</span>]-&gt;score);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>一般来说应该会报这样的一个错误，而且是有一个报一个😂，能给报一堆：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">test.c: In function ‘main’:</span><br><span class="line">test.c:20:47: error: invalid type argument of ‘-&gt;’ (have ‘struct Student’)</span><br><span class="line">     printf(&quot;%s\t  %c\t%d\t%s\t%.2f\n&quot;, pStu[0]-&gt;name, pStu[0]-&gt;gender, pStu[0]-&gt;age, pStu[0]-&gt;id, pStu[0]-&gt;score);</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-4结构体指针与函数"><a href="#2-4结构体指针与函数" class="headerlink" title="2.4结构体指针与函数"></a><font size=3>2.4结构体指针与函数</font></h3><p>结构体变量名代表的是整个集合本身，作为函数参数时传递的整个集合，也就是所有成员，而不是像数组一样被编译器转换成一个指针。</p>
<p>如果结构体成员较多，尤其是成员为数组时，传送的时间和空间开销会很大，影响程序的运行效率。所以最好的办法就是使用结构体指针，这时由实参传向形参的只是一个地址，速度将会有所提升。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义结构体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Student</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="type">char</span> *name;      <span class="comment">/* 姓名 */</span></span><br><span class="line">    <span class="type">char</span> gender;     <span class="comment">/* 性别 */</span></span><br><span class="line">    <span class="type">int</span> age;         <span class="comment">/* 年龄 */</span></span><br><span class="line">    <span class="type">char</span> id[<span class="number">11</span>];     <span class="comment">/* 学号 */</span></span><br><span class="line">    <span class="type">float</span> score;     <span class="comment">/* 成绩 */</span></span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">average</span><span class="params">(<span class="keyword">struct</span> Student *ps, <span class="type">int</span> len)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> <span class="title">stu</span>[3] =</span> &#123;&#123;<span class="string">&quot;fanhua&quot;</span>, <span class="string">&#x27;g&#x27;</span>, <span class="number">18</span>, <span class="string">&quot;0000000003&quot;</span>, <span class="number">97.5</span>&#125;,</span><br><span class="line">                             &#123;<span class="string">&quot;qidaink&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">19</span>, <span class="string">&quot;0000000001&quot;</span>, <span class="number">95.67</span>&#125;,</span><br><span class="line">                             &#123;<span class="string">&quot;a&quot;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="number">16</span>, <span class="string">&quot;0000000002&quot;</span>, <span class="number">87.5</span>&#125;&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Student</span> *<span class="title">pStu</span> =</span> stu;</span><br><span class="line">    <span class="type">int</span> len = <span class="keyword">sizeof</span>(stu) / <span class="keyword">sizeof</span>(<span class="keyword">struct</span> Student);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Name\tGender\tAge\t    ID\t\tScore\t\n&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span>(pStu = stu; pStu &lt; stu + len; pStu++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%s\t  %c\t%d\t%s\t%.2f\n&quot;</span>, pStu-&gt;name, pStu-&gt;gender, pStu-&gt;age, pStu-&gt;id, pStu-&gt;score);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    average(stu, len);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">average</span><span class="params">(<span class="keyword">struct</span> Student *ps, <span class="type">int</span> len)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">    <span class="type">float</span> average, sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; len; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        sum += (ps + i) -&gt; score;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    average = sum / len;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;average = %.2f\n&quot;</span>, average);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Name    Gender  Age         ID          Score</span><br><span class="line">fanhua    g     18      0000000003      97.50</span><br><span class="line">qidaink   m     19      0000000001      95.67</span><br><span class="line">a         m     16      0000000002      87.50</span><br><span class="line">average = 93.56</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="四、枚举类型"><a href="#四、枚举类型" class="headerlink" title="四、枚举类型"></a><font size=3>四、枚举类型</font></h1><p>在实际问题中，有些数据的取值往往是有限的，只能是非常少量的整数，并且最好为每个值都取一个名字，以方便在后续代码中使用，比如一个星期只有七天，一年只有十二个月，等。</p>
<p>针对上边那些特殊变量，  C  语言为我们提供了枚举类型，在枚举的定义中，会将这些变量的值一一列举出来，并且枚举类型的变量的值就只限于列举出来的值的范围。</p>
<h2 id="1-定义枚举类型"><a href="#1-定义枚举类型" class="headerlink" title="1.定义枚举类型"></a><font size=3>1.定义枚举类型</font></h2><p>枚举类型的定义需要用到关键字  enum  ，一般定义格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">typeName</span></span></span><br><span class="line"><span class="class">&#123;</span> </span><br><span class="line">    valueName1, </span><br><span class="line">    valueName2, </span><br><span class="line">    valueName3, </span><br><span class="line">    ... </span><br><span class="line">    valueNameN, </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>  typeName  为枚举类型的名称，  valueNameN  为枚举类型的成员。</p>
<p>【注意】</p>
<p>（1）枚举类型中任意两个<strong>枚举成员不能具有相同的名称</strong>。而且枚举列表中的  valueName1…  这些标识符的<strong>作用范围是全局的</strong>（严格来说是   main()   函数内部），<strong>不能再定义与它们名字相同的变量</strong>。</p>
<p>（2）枚举类型中间的枚举成员之间用逗号（  ,  ）隔开，定义结束时，分号（  ;  ）必不可少。</p>
<p>（3）在枚举类型中，<strong>枚举成员的类型只能是整型</strong>，并且声明的第一个枚举成员默认值是  0  ，往后逐个加   1  （递增）。这样增加后的值必须是整型可表示的值得范围，否则会报错。</p>
<p>（4）我们也可以在定义枚举类型时，为枚举成员显示赋值，允许多个枚举成员具有相同的值。没有显示赋值的枚举成员的值总是前一个枚举成员的值加  1  。例如，</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    Monday = <span class="number">1</span>,</span><br><span class="line">    Tuesday = <span class="number">2</span>,</span><br><span class="line">    Wednesday,</span><br><span class="line">    Thursday,</span><br><span class="line">    Friday,</span><br><span class="line">    Saturday,</span><br><span class="line">    Sunday</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>（5）枚举与宏其实有些类似，宏在预处理阶段将名字替换成对应的值，枚举在编译阶段将名字替换成对应的值。我们可以将枚举理解为编译阶段的宏。</p>
<p>当我们的枚举类型定义完成后，<strong>枚举成员都是常量</strong>，它们不占用数据区（常量区、全局数据区、栈区和堆区）的内存，而是<strong>直接被编译到命令里面</strong>，<strong>放到代码区</strong>，所以不能用    &amp;    取得它们的地址。我们也无法修改枚举成员的值，除非直接修改相应枚举类型的定义。</p>
<h2 id="2-定义枚举变量"><a href="#2-定义枚举变量" class="headerlink" title="2.定义枚举变量"></a><font size=3>2.定义枚举变量</font></h2><p>与结构体一样，枚举也是一种构造的数据类型，所以可以像结构体一样定义枚举变量。枚举变量定义一般格式如下：</p>
<div class="tabs" id="tabname7"><ul class="nav-tabs"><li class="tab active"><a href="#tabname7-1">方式一</a></li><li class="tab"><a href="#tabname7-2">方式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname7-1"><ul>
<li>先定义枚举数据类型，再声明变量</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义枚举数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    Monday = <span class="number">1</span>,</span><br><span class="line">    Tuesday = <span class="number">2</span>,</span><br><span class="line">    Wednesday,</span><br><span class="line">    Thursday,</span><br><span class="line">    Friday,</span><br><span class="line">    Saturday,</span><br><span class="line">    Sunday</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义枚举类型变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span> <span class="title">w1</span>, <span class="title">w2</span>;</span></span><br></pre></td></tr></table></figure>

<p>定义了两个枚举变量   w1   和   w2  。</p>
<p>【注意】关键字  Weeks  不可以省略，若没有这个关键字，则系统不认为  Weeks   是枚举类型。</p></div><div class="tab-pane" id="tabname7-2"><ul>
<li>定义枚举类型的同时声明枚举变量</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义枚举类型的同时声明枚举变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    Monday = <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    Tuesday = <span class="string">&quot;2&quot;</span>,</span><br><span class="line">    Wednesday,</span><br><span class="line">    Thursday,</span><br><span class="line">    Friday,</span><br><span class="line">    Saturday,</span><br><span class="line">    Sunday</span><br><span class="line">&#125;w1, w2;</span><br></pre></td></tr></table></figure>

<p>与方式一类似，定义了两个枚举变量   w1   和   w2  。</p>
<p>【说明】如果只需要   w1  、  w2   两个变量，<strong>后面不需要再使用枚举类型名称定义其他变量，那么在定义时也可以不给出枚举类型名称</strong>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义枚举类型的同时声明枚举变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">enum</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    Monday = <span class="string">&quot;1&quot;</span>,</span><br><span class="line">    Tuesday = <span class="string">&quot;2&quot;</span>,</span><br><span class="line">    Wednesday,</span><br><span class="line">    Thursday,</span><br><span class="line">    Friday,</span><br><span class="line">    Saturday,</span><br><span class="line">    Sunday</span><br><span class="line">&#125;w1, w2;</span><br></pre></td></tr></table></figure></div></div></div>



<h2 id="3-枚举变量的使用"><a href="#3-枚举变量的使用" class="headerlink" title="3.枚举变量的使用"></a><font size=3>3.枚举变量的使用</font></h2><h3 id="3-1引用枚举成员"><a href="#3-1引用枚举成员" class="headerlink" title="3.1引用枚举成员"></a><font size=3>3.1引用枚举成员</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">枚举类型名<span class="number">1</span></span><br><span class="line">枚举类型名<span class="number">2</span></span><br><span class="line">...</span><br><span class="line">枚举类型名N</span><br></pre></td></tr></table></figure>

<h3 id="3-2枚举变量赋值"><a href="#3-2枚举变量赋值" class="headerlink" title="3.2枚举变量赋值"></a><font size=3>3.2枚举变量赋值</font></h3><p>枚举类型变量赋值方式有两种，接下来通过实例说明。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <div class="tabs" id="tabname8"><ul class="nav-tabs"><li class="tab active"><a href="#tabname8-1">赋值方式一</a></li><li class="tab"><a href="#tabname8-2">赋值方式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname8-1"><ul><li>先定义枚举类型变量，再赋值</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义枚举数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    Monday = <span class="number">1</span>,</span><br><span class="line">    Tuesday = <span class="number">2</span>,</span><br><span class="line">    Wednesday,</span><br><span class="line">    Thursday,</span><br><span class="line">    Friday,</span><br><span class="line">    Saturday,</span><br><span class="line">    Sunday</span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span> <span class="title">w1</span>, <span class="title">w2</span>, <span class="title">w3</span>;</span></span><br><span class="line">    w1 = Monday;</span><br><span class="line">    w2 = Sunday;</span><br><span class="line">    w3 = <span class="number">9</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d %d %d %d %d %d %d\n&quot;</span>, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d %d %d \n&quot;</span>, w1, w2, w3);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7</span><br><span class="line">1 7 9</span><br></pre></td></tr></table></figure><p>【说明】我们可以把定义枚举类类型时的枚举成员赋值给枚举变量，也可以直接把整数值赋给枚举变量。枚举变量其实也就是一种<strong>整型变量</strong>。</p></div><div class="tab-pane" id="tabname8-2"><ul><li>定义枚举变量的同时进行赋值</li></ul><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="comment">/* 定义枚举数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    Monday = <span class="number">1</span>,</span><br><span class="line">    Tuesday = <span class="number">2</span>,</span><br><span class="line">    Wednesday,</span><br><span class="line">    Thursday,</span><br><span class="line">    Friday,</span><br><span class="line">    Saturday,</span><br><span class="line">    Sunday</span><br><span class="line">&#125;;                   <span class="comment">/* ; 不可缺少 */</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Weeks</span> <span class="title">w1</span> =</span> Monday, w2 = Friday, w3 = <span class="number">10</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d %d %d %d %d %d %d\n&quot;</span>, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d %d %d \n&quot;</span>, w1, w2, w3);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1 2 3 4 5 6 7</span><br><span class="line">1 5 10</span><br></pre></td></tr></table></figure></div></div></div>
              </div>
            </details>

<h1 id="五、共用体"><a href="#五、共用体" class="headerlink" title="五、共用体"></a><font size=3>五、共用体</font></h1><p>在  C  语言中，不同数据类型的数据可以<strong>使用共同的存储区域</strong>，这种数据构造类型称为共用体，简称<strong>共用</strong>，又称<strong>联合体</strong>。共用体在定义、说明和使用形式上与结构体相似。<strong>两者本质上的不同仅在于使用内存的方式上</strong>。 </p>
<h2 id="1-定义共用体"><a href="#1-定义共用体" class="headerlink" title="1.定义共用体"></a><font size=3>1.定义共用体</font></h2><p>定义一个结构体数据类型，我们需要用到  union  关键字：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">union_name</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    data_type member_name1;</span><br><span class="line">    data_type member_name2;</span><br><span class="line">    ...</span><br><span class="line">    data_type member_nameN;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看各部分说明 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=70px>union</td>        <td align="left">共用体的关键字，表明这是一个共用体的定义</td>    </tr>    <tr>        <td align="center" width=70px>union_name</td>        <td align="left">共用体的名称，与普通变量名要求一样</td>    </tr>    <tr>        <td align="center" width=70px>data_type</td>        <td align="left">共用体内成员的数据类型，可以是基本变量类型，指针类型，枚举类型，联合体类型或者结构体类型等。比如，char，int等</td>    </tr>    <tr>        <td align="center" width=70px>member_nameN</td>        <td align="left">共用体内成员的名称（成员变量名），命名规则与变量相同</td>    </tr></table>
              </div>
            </details>

<p>【注意】</p>
<p>（1）共用体的所有成员占用同一段内存，修改一个成员会影响其余所有成员。</p>
<p>（2）共用体<strong>占用的内存等于最长的成员占用的内存</strong>。</p>
<p>（3）<strong>共用体使用了内存覆盖技术，</strong>同一时刻只能保存一个成员的值，如果对新的成员赋值，就会把原来成员的值覆盖掉。</p>
<h2 id="2-共用体的长度"><a href="#2-共用体的长度" class="headerlink" title="2.共用体的长度"></a><font size=3>2.共用体的长度</font></h2><p>我们可以使用  sizeof  函数来求得所定义共用体的长度。共用体既然共用同一段内存区域，那么它的长度就应该<strong>等于共用体成员中最长的数据类型的长度</strong>。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义共用体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">data1</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">char</span> a;</span><br><span class="line">    <span class="type">short</span> b;</span><br><span class="line">    <span class="type">int</span> c;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">data2</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">char</span> a;</span><br><span class="line">    <span class="type">short</span> b;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">union</span> <span class="title">data1</span> <span class="title">temp1</span>;</span> <span class="comment">/* 定义共用体变量 */</span></span><br><span class="line">    <span class="class"><span class="keyword">union</span> <span class="title">data2</span> <span class="title">temp2</span>;</span> <span class="comment">/* 定义共用体变量 */</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(temp1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(temp1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(union data1) = %ld\n\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">union</span> data1));</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(temp2) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(temp2));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(union data2) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">union</span> data2));</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sizeof(temp1) = 4</span><br><span class="line">sizeof(union data1) = 4</span><br><span class="line"></span><br><span class="line">sizeof(temp2) = 2</span><br><span class="line">sizeof(union data2) = 2</span><br></pre></td></tr></table></figure><p>我们会发现（我用的  linux  是  64  位的），总的大小是等于最长的数据所占空间大小的。</p>
              </div>
            </details>

<h2 id="3-在内存中的存储"><a href="#3-在内存中的存储" class="headerlink" title="3.在内存中的存储"></a><font size=3>3.在内存中的存储</font></h2><p>既然共用体的成员共用一段内存空间，那打印地址的话，他们怎么分布呢？</p>
<details class="folding-tag" blue><summary> 点击查看测试实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义共用体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">char</span> a;</span><br><span class="line">    <span class="type">short</span> b;</span><br><span class="line">    <span class="type">int</span> c;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">union</span> <span class="title">data</span> <span class="title">temp</span>;</span> <span class="comment">/* 定义共用体变量 */</span></span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&amp;temp   = %p\n&quot;</span>, &amp;temp);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&amp;temp.a = %p\n&quot;</span>, &amp;temp.a);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&amp;temp.b = %p\n&quot;</span>, &amp;temp.b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&amp;temp.c = %p\n&quot;</span>, &amp;temp.c);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">sizeof(temp) = 4</span><br><span class="line">sizeof(union data) = 4</span><br><span class="line">&amp;temp   = 0x7ffc7c3d2b14</span><br><span class="line">&amp;temp.a = 0x7ffc7c3d2b14</span><br><span class="line">&amp;temp.b = 0x7ffc7c3d2b14</span><br><span class="line">&amp;temp.c = 0x7ffc7c3d2b14</span><br></pre></td></tr></table></figure><p>我们会发现他们的地址都是同一个。</p>
              </div>
            </details>

<p>从例子中不难看出，这几个成员的地址都是一样的，他们确实共用同一段内存。</p>
<h2 id="4-赋值？"><a href="#4-赋值？" class="headerlink" title="4.赋值？"></a><font size=3>4.赋值？</font></h2><p>看到这里，我会想，它跟结构体用法一样，但是却又共用同一段内存，那我还能给它赋值嘛？接下来我们就开看一看成员赋值时的变化情况。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义共用体数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">char</span> a;</span><br><span class="line">    <span class="type">short</span> b;</span><br><span class="line">    <span class="type">int</span> c;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">union</span> <span class="title">data</span> <span class="title">temp</span>;</span> <span class="comment">/* 定义共用体变量 */</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(temp) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(temp));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(union data) = %ld\n\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">union</span> data));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&amp;temp.a = %p,&amp;temp.b = %p,&amp;temp.b = %p\n&quot;</span>, &amp;temp.a, &amp;temp.b, &amp;temp.c);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;temp.a\t\ttemp.b\t\ttemp.c\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c(%#x)\t%#hx\t\t%d(%#x)\n&quot;</span>, temp.a, temp.a, temp.b, temp.c, temp.c);</span><br><span class="line">    temp.c = <span class="number">0x40</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c(%#x)\t\t%#hx\t\t%d(%#x)\n&quot;</span>, temp.a, temp.a, temp.b, temp.c, temp.c);</span><br><span class="line">    temp.a = <span class="string">&#x27;9&#x27;</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c(%#x)\t\t%#hx\t\t%d(%#x)\n&quot;</span>, temp.a, temp.a, temp.b, temp.c, temp.c);</span><br><span class="line">    temp.b = <span class="number">0x2059</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c(%#x)\t\t%#hx\t\t%d(%#x)\n&quot;</span>, temp.a, temp.a, temp.b, temp.c, temp.c);</span><br><span class="line">    temp.c = <span class="number">0x3e25ad54</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c(%#x)\t\t%#hx\t\t%d(%#x)\n&quot;</span>, temp.a, temp.a, temp.b, temp.c, temp.c);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在终端执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure>

<p>会看到有如下信息输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">sizeof(temp) = 4</span><br><span class="line">sizeof(union data) = 4</span><br><span class="line"></span><br><span class="line">&amp;temp.a = 0x7ffc18626194,&amp;temp.b = 0x7ffc18626194,&amp;temp.b = 0x7ffc18626194</span><br><span class="line">temp.a          temp.b          temp.c</span><br><span class="line">�(0xfffffffc)   0x7ffc          32764(0x7ffc)</span><br><span class="line">@(0x40)         0x40            64(0x40)</span><br><span class="line">9(0x39)         0x39            57(0x39)</span><br><span class="line">Y(0x59)         0x2059          8281(0x2059)</span><br><span class="line">T(0x54)         0xad54          1042656596(0x3e25ad54)</span><br></pre></td></tr></table></figure>

<p>为什么要先对整型成员赋值呢？这是因为整型成员赋值后可以将这一个字节都给初始化掉，未初始化的时候谁也不知道里边是什么，这也是为了保证结果的准确性。接下来我们来分析一下数据的变化情况。</p>
<p>【说明】红色字体为每次变化的位置。</p>
<ul>
<li>temp.c &#x3D; 0x40;  赋值后</li>
</ul>
<table>
    <tr>
        <td align="center">0x7ffc18626194</td>
        <td align="center">0x7ffc18626195</td>
        <td align="center">0x7ffc18626196</td>
        <td align="center">0x7ffc18626197</td>
    </tr>
    <tr>
        <td align="center" style="color: red;">0x40</td>
        <td align="center" style="color: red;">0x00</td>
        <td align="center" style="color: red;">0x00</td>
        <td align="center" style="color: red;">0x00</td>
    </tr>
    <tr>
        <td align="center">temp.a = 0x40</td>
        <td align="center" colspan="3"></td>
    </tr>
    <tr>
        <td align="center" colspan="2">temp.b = 0x40</td>
        <td align="center" colspan="2"></td>
    </tr>
    <tr>
        <td align="center" colspan="4" style="color: red;">temp.c = 0x40</td>
    </tr>
</table>


<ul>
<li>temp.a &#x3D; ‘9’;  赋值后</li>
</ul>
<table>
    <tr>
        <td align="center">0x7ffc18626194</td>
        <td align="center">0x7ffc18626195</td>
        <td align="center">0x7ffc18626196</td>
        <td align="center">0x7ffc18626197</td>
    </tr>
    <tr>
        <td align="center" style="color: red;">0x39</td>
        <td align="center">0x00</td>
        <td align="center">0x00</td>
        <td align="center">0x00</td>
    </tr>
    <tr>
        <td align="center" style="color: red;">temp.a = '9' (ASCII 为0x39)</td>
        <td align="center" colspan="3"></td>
    </tr>
    <tr>
        <td align="center" colspan="2">temp.b = 0x39</td>
        <td align="center" colspan="2"></td>
    </tr>
    <tr>
        <td align="center" colspan="4">temp.c = 0x39</td>
    </tr>
</table>


<ul>
<li>temp.b &#x3D; 0x2059;  赋值后</li>
</ul>
<table>
    <tr>
        <td align="center">0x7ffc18626194</td>
        <td align="center">0x7ffc18626195</td>
        <td align="center">0x7ffc18626196</td>
        <td align="center">0x7ffc18626197</td>
    </tr>
    <tr>
        <td align="center" style="color: red;">0x59</td>
        <td align="center" style="color: red;">0x20</td>
        <td align="center">0x00</td>
        <td align="center">0x00</td>
    </tr>
    <tr>
        <td align="center">temp.a = 0x59</td>
        <td align="center" colspan="3"></td>
    </tr>
    <tr>
        <td align="center" colspan="2" style="color: red;">temp.b = 0x2059</td>
        <td align="center" colspan="2"></td>
    </tr>
    <tr>
        <td align="center" colspan="4">temp.c = 0x39</td>
    </tr>
</table>


<ul>
<li>temp.c &#x3D; 0x3e25ad54;  赋值后</li>
</ul>
<table>
    <tr>
        <td align="center">0x7ffc18626194</td>
        <td align="center">0x7ffc18626195</td>
        <td align="center">0x7ffc18626196</td>
        <td align="center">0x7ffc18626197</td>
    </tr>
    <tr>
        <td align="center" style="color: red;">0x54</td>
        <td align="center" style="color: red;">0xad</td>
        <td align="center" style="color: red;">0x25</td>
        <td align="center" style="color: red;">0x3e</td>
    </tr>
    <tr>
        <td align="center">temp.a = 0x54</td>
        <td align="center" colspan="3"></td>
    </tr>
    <tr>
        <td align="center" colspan="2">temp.b = 0xad54</td>
        <td align="center" colspan="2"></td>
    </tr>
    <tr>
        <td align="center" colspan="4" style="color: red;">temp.c = 0x3e25ad54</td>
    </tr>
</table>


<p>成员   a  、  b  、  c   在内存中“对齐”到一头，对   a   赋值修改的是前一个字节，对   b   赋值修改的是前两个字节，对   c   赋值修改的是全部字节。也就是说，  a  、  b   会影响到   c   的一部分数据，而   c   会影响到   a  、  b   的全部数据。</p>
<p>根据  temp.a  、  temp.b  和  temp.c  的分析，可以得出数据在内存中的存储形式。按照最后一次整型的赋值，我们再通过根据  temp.a  、  temp.b  的值，就可以分析出这四个字节的数据的存储是符合小端模式的，高字节存储在高地址，低字节存储在低位，同时这也是一种<strong>判定大小端</strong>的方式（大小端可以看后边的关于大小端的笔记）。</p>
<h1 id="六、位域"><a href="#六、位域" class="headerlink" title="六、位域"></a><font size=3>六、位域</font></h1><h2 id="1-什么是位域"><a href="#1-什么是位域" class="headerlink" title="1.什么是位域"></a><font size=3>1.什么是位域</font></h2><p>计算机的内存是以字节为单位，为变量分配内存，也是以字书为单位。但是，实际上，有些信息的存储，并不需要占用一个字节，只需要   1  个或几个二进制位就够了。</p>
<p>比如：人的性别，只有两种可能的取值男和女，我们就可以用  0   表示男，  1  表示女，这样使用  1   个二进制位就够了。多数情况，我们会选择  char   类型，占用  1  个字节，但是如果可以用一个位来表示，那有何必浪费  8  个位呢，毕竟在一些内存很小的  CPU  中，内存空间是非常珍贵的。</p>
<p>为了节省存储空间，  C  语言中又提供了一种数据结构，称为<strong>位域</strong>或<strong>位段</strong>， </p>
<p>所谓位域是把<strong>一个字节中的二进位划分为几个不同的区域</strong>，并说明每个区域的位数。每个域有一个域名，<strong>允许在程序中按域名进行操作</strong>。这样就可以把几个不同的对象用一个字节的二进制位域来表示。</p>
<p>【<strong>位域的本质</strong>】本质其实就是一种结构类型，只不过成员是按照二进制位进行分配的。</p>
<p>【注意】位域成员往往不占用完整的字节，有时候也不处于字节的开头位置，因此使用  &amp;  获取位域成员的地址是没有意义的，C语言也禁止这样做。地址是字节（  Byte  ）的编号，而不是位（  Bit  ）的编号。</p>
<h2 id="2-定义位域"><a href="#2-定义位域" class="headerlink" title="2.定义位域"></a><font size=3>2.定义位域</font></h2><p>作为一种数据类型，位域的定义与结构体很类似，都是使用关键字  struct  ，一般格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bitField_name</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    data_type member_name1: len1;</span><br><span class="line">    data_type member_name2: len2;</span><br><span class="line">    ...</span><br><span class="line">    data_type member_nameN: lenN;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看各部分说明 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=70px>struct</td>        <td align="left">位域定义的关键字，表明这是一个位域的定义</td>    </tr>    <tr>        <td align="center" width=70px>structure_name</td>        <td align="left">位域解耦股的名称，与普通变量名要求一样</td>    </tr>    <tr>        <td align="center" width=70px>data_type</td>        <td align="left">位域内成员的数据类型。</td>    </tr>    <tr>        <td align="center" width=70px>member_nameN</td>        <td align="left">位域内成员的名称（成员变量名），命名规则与变量相同</td>    </tr>    <tr>        <td align="center" width=70px>lenN</td>        <td align="left">位域内成员的长度（以bit为单位）</td>    </tr></table>
              </div>
            </details>

<p>【说明】</p>
<p>（1）位域更像是定义在结构体内部的一种特殊的成员，在定义结构体的时候可以直接定义位域成员。例如，</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span>&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> m;</span><br><span class="line">    <span class="type">unsigned</span> n: <span class="number">4</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> ch: <span class="number">6</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>  :  后面的数字用来限定成员变量占用的位数。成员   m   没有限制，根据数据类型即可推算出它占用   4   个字节（  Byte  ）的内存。成员   n  、  ch   被  :  后面的数字限制，不能再根据数据类型计算长度，它们分别占用   4  、  6   位（  Bit  ）的内存。</p>
<p>（2）位域的占用位数不能超过  8  个二进制位，同时位域也不允许跨字节。</p>
<p>（3）允许位域没有域名，只给出数据类型和位宽。没有域名的位域无名位域一般用来作填充或者调整成员位置。需要注意的是没有名称的域名是无法使用的。例如，</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span>&#123;</span></span><br><span class="line">    <span class="type">int</span> m: <span class="number">12</span>;</span><br><span class="line">    <span class="type">int</span>  : <span class="number">20</span>;  <span class="comment">/* 该位域成员不能使用 */</span></span><br><span class="line">    <span class="type">int</span> n: <span class="number">4</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>如果没有位宽为   20   的无名成员，  m  、  n   将会挨着存储，  sizeof(struct data)   的结果为   4  ；有了这   20   位作为填充，  m  、  n   将分开存储，  sizeof(struct data)   的结果为   8  ，具体为什么，后边再分析。</p>
<p>（4）  C  语言标准还规定，只有有限的几种数据类型可以用于位域。在   ANSI C   中，这几种数据类型是   int  、  signed int   和   unsigned int  （  int   默认就是   signed int  ）；到了   C99  ，  _Bool   也被支持了。</p>
<h2 id="3-定义位域变量"><a href="#3-定义位域变量" class="headerlink" title="3.定义位域变量"></a><font size=3>3.定义位域变量</font></h2><p>位域也是一种数据类型，所以自然也可以用于定义变量，位域变量的定义与结构体一样。</p>
<div class="tabs" id="tabname9"><ul class="nav-tabs"><li class="tab active"><a href="#tabname9-1">方式一</a></li><li class="tab"><a href="#tabname9-2">方式二</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname9-1"><ul>
<li>先定义位域数据类型，再声明位域变量</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">d1</span>, <span class="title">d2</span>;</span></span><br></pre></td></tr></table></figure>

<p>定义了两个位域变量   d1   和   d2  ，它们都是   data   位域类型，都由   3   个成员组成。</p>
<p>【注意】</p>
<p>（1）关键字  struct  不可以省略，若没有这个关键字，则系统不认为  data   是位域类型。</p>
<p>（2）  data  就像一个“模板”，定义出来的变量都<strong>具有相同的性质</strong>。</p></div><div class="tab-pane" id="tabname9-2"><ul>
<li>定义位域数据类型的同时声明位域变量</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义位域数据类型同时声明位域变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;d1, d2;</span><br></pre></td></tr></table></figure>

<p>与方式一类似，定义了两个位域变量   d1   和   d2  ，它们都是   data   位域类型，都由   3   个成员组成。</p>
<p>【说明】如果只需要   d1  、  d2   两个变量，<strong>后面不需要再使用位域结构名称定义其他变量，那么在定义时也可以不给出位域结构名称</strong>。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 定义结位域数据类型同时声明位域变量 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;d1, d2;</span><br></pre></td></tr></table></figure></div></div></div>

<h2 id="4-位域变量的使用"><a href="#4-位域变量的使用" class="headerlink" title="4.位域变量的使用"></a><font size=3>4.位域变量的使用</font></h2><p>位域成员的使用和结构体成员的使用方式相同，可以对位域变量成员进行引用和赋值，这些操作都是通过成员运算符    .    完成的。</p>
<ul>
<li>引用位域变量成员的方式如下：</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">位域变量名.位域名<span class="number">1</span></span><br><span class="line">位域变量名.位域名<span class="number">2</span></span><br><span class="line">...</span><br><span class="line">位域变量名.位域名N</span><br></pre></td></tr></table></figure>

<ul>
<li>对位域变量成员赋值的方式如下：</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">位域变量名.位域名<span class="number">1</span> = value1;</span><br><span class="line">位域变量名.位域名<span class="number">2</span> = value2;</span><br><span class="line">...</span><br><span class="line">位域变量名.位域名N = valueN;</span><br></pre></td></tr></table></figure>

<p>【注意】赋值时不要超出位域定义时的位数。</p>
<div class="tabs" id="tabname10"><ul class="nav-tabs"><li class="tab active"><a href="#tabname10-1">赋值方式一</a></li><li class="tab"><a href="#tabname10-2">赋值方式二</a></li><li class="tab"><a href="#tabname10-3">赋值方式三</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname10-1"><ul>
<li>先定义位域变量，再赋值</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">d1</span>;</span> <span class="comment">/* 定义位域变量 */</span></span><br><span class="line">    d1.a = <span class="number">1</span>; <span class="comment">/* 0000 0001 */</span></span><br><span class="line">    d1.b = <span class="number">6</span>; <span class="comment">/* 0000 0110 */</span></span><br><span class="line">    d1.c = <span class="number">13</span>;<span class="comment">/* 0000 1101 */</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\t%d\t%d\n&quot;</span>, d1.a, d1.b, d1.c);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在终端执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure>

<p>会看到有如下信息输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1       6       13</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname10-2"><ul>
<li>先定义位域，然后在定义位域变量的时候进行赋值</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">d1</span> =</span> &#123;<span class="number">1</span>, <span class="number">6</span>, <span class="number">13</span>&#125;; <span class="comment">/* 定义位域变量 1:0000 0001 6:0000 0110 13:0000 1101*/</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\t%d\t%d\n&quot;</span>, d1.a, d1.b, d1.c);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在终端执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure>

<p>会看到有如下信息输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1       6       13</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname10-3"><ul>
<li>定义结构体时直接对结构体变量赋值</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;d1 = &#123;<span class="number">1</span>, <span class="number">6</span>, <span class="number">13</span>&#125;;</span><br><span class="line"><span class="comment">/* 定义位域变量 1:0000 0001 6:0000 0110 13:0000 1101*/</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\t%d\t%d\n&quot;</span>, d1.a, d1.b, d1.c);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在终端执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure>

<p>会看到有如下信息输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1       6       13</span><br></pre></td></tr></table></figure></div></div></div>

<h2 id="5-位域的长度"><a href="#5-位域的长度" class="headerlink" title="5.位域的长度"></a><font size=3>5.位域的长度</font></h2><h3 id="5-1长度计算"><a href="#5-1长度计算" class="headerlink" title="5.1长度计算"></a><font size=3>5.1长度计算</font></h3><p>当我们定义了一个位域后，用它定义出来的位域变量占多大空间呢？我们可以使用  sizeof  函数来求得所定义位域数据类型的长度。一般格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">sizeof</span>( <span class="keyword">struct</span> bitField_name) </span><br><span class="line"><span class="comment">/* 或者使用结构体变量来求 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bitField_name</span> <span class="title">bitField_variable</span>;</span></span><br><span class="line"><span class="keyword">sizeof</span>(bitField_variable) </span><br></pre></td></tr></table></figure>

<p>【注意】我们无法使用该函数求位域内成员的长度，即便位域成员长度超过一个字节，也无法使用，一般会报如下错误：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">test.c:18:47: error: cannot take address of bit-field ‘a’</span><br></pre></td></tr></table></figure>

<h3 id="5-2使用实例"><a href="#5-2使用实例" class="headerlink" title="5.2使用实例"></a><font size=3>5.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">3</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">d1</span> =</span> &#123;<span class="number">1</span>, <span class="number">6</span>, <span class="number">13</span>&#125;;<span class="comment">/* 定义位域变量 1:0000 0001 6:0000 0110 13:0000 1101*/</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(unsigned int) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="type">unsigned</span> <span class="type">int</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(d1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(d1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(struct data) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> data));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d\t%d\t%d\n&quot;</span>, d1.a, d1.b, d1.c);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sizeof(unsigned int) = 4</span><br><span class="line">sizeof(d1) = 4</span><br><span class="line">sizeof(struct data) = 4</span><br><span class="line">1       6       13</span><br></pre></td></tr></table></figure><p>具体的那我们就接着往后看。存储方式，我们下节会通过实例来说明。</p>
              </div>
            </details>

<h2 id="6-在内存中的存储"><a href="#6-在内存中的存储" class="headerlink" title="6.在内存中的存储"></a><font size=3>6.在内存中的存储</font></h2><h3 id="6-1非法操作？"><a href="#6-1非法操作？" class="headerlink" title="6.1非法操作？"></a><font size=3>6.1非法操作？</font></h3><p>当位域数据类型定义了一个位域变量的时候，这个位域以及其成员变量在内存中是怎样的呢？我么是否可以取出位域成员的地址来分析呢？</p>
<p>在  C  语言中，我们可以得到某个字节的内存地址，我们具备了操作任意内存字节的能力；但是在内存空间稀缺的年代，仅仅控制到字节级别还不足以满足  C   程序员的需求，为此  C  语言中又出现了  bit  级别内存的“有限操作能力” —— 位域。这里所谓的“有限”指的是机器的最小粒度寻址单位是字节，我们无法像获得某个字节地址那样得到某个  bit  的地址，因此我们仅能通过字节的运算来设置和获取某些  bit  的值。<strong>在C语言中，尝试获得一个bit field的地址是非法操作</strong>。</p>
<h3 id="6-2存储规则"><a href="#6-2存储规则" class="headerlink" title="6.2存储规则"></a><font size=3>6.2存储规则</font></h3><p>在  C  语言标准中并没有规定位域的具体存储方式，不同的编译器有不同的实现，但它们都<strong>尽量压缩存储空间</strong>。</p>
<div class="tabs" id="tabname11"><ul class="nav-tabs"><li class="tab active"><a href="#tabname11-1">规则一</a></li><li class="tab"><a href="#tabname11-2">规则二</a></li><li class="tab"><a href="#tabname11-3">规则三</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname11-1"><p>当相邻成员的类型相同时，如果它们的位宽之和小于类型的   sizeof   大小，那么后面的成员紧邻前一个成员存储，直到不能容纳为止；如果它们的位宽之和大于类型的   sizeof   大小，那么后面的成员将从新的存储单元开始，其偏移量为类型大小的整数倍。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">6</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">12</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">4</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">d1</span>;</span><span class="comment">/* 定义位域变量 1:0000 0001 6:0000 0110 13:0000 1101*/</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(unsigned int) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="type">unsigned</span> <span class="type">int</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(d1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(d1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(struct data) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> data));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">sizeof(unsigned int) = 4</span><br><span class="line">sizeof(d1) = 4</span><br><span class="line">sizeof(struct data) = 4</span><br></pre></td></tr></table></figure><p>  a  、  b  、  c   的类型都是   unsigned int  ，  sizeof   的结果为   4   个字节（  Byte  ），也就是   32   个位（  Bit  ）。  a  、  b  、  c   的位宽之和为   6+12+4 &#x3D; 22  ，小于   32  ，所以它们会挨着存储，中间没有缝隙。</p><blockquote><p> sizeof(struct data)   的大小之所以为   4  ，而不是   3  ，是因为要将内存对齐到   4   个字节，以便提高存取效率。</p></blockquote><p>如果将成员 a 的位宽改为 22，那么输出结果将会是 8，因为 22+12 &#x3D; 34，大于 32，b 会从新的位置开始存储，相对 a 的偏移量是 sizeof(unsigned int)，也即 4 个字节。</p><p>如果再将成员 c 的位宽也改为 22，那么输出结果将会是 12，三个成员都不会挨着存储。</p>
              </div>
            </details></div><div class="tab-pane" id="tabname11-2"><p> 当相邻成员的类型不同时，不同的编译器有不同的实现方案，  GCC  会压缩存储，而   VC&#x2F;VS   不会。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">12</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> b: <span class="number">4</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">12</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">d1</span>;</span><span class="comment">/* 定义位域变量 */</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(unsigned int) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="type">unsigned</span> <span class="type">int</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(unsigned char) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="type">unsigned</span> <span class="type">char</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(d1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(d1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(struct data) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> data));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sizeof(unsigned int) = 4</span><br><span class="line">sizeof(unsigned char) = 1</span><br><span class="line">sizeof(d1) = 4</span><br><span class="line">sizeof(struct data) = 4</span><br></pre></td></tr></table></figure>
              </div>
            </details></div><div class="tab-pane" id="tabname11-3"><p>如果成员之间穿插着非位域成员，那么不会进行压缩。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">12</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> b;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">12</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span> <span class="title">d1</span>;</span><span class="comment">/* 定义位域变量 */</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(unsigned int) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="type">unsigned</span> <span class="type">int</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(unsigned char) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="type">unsigned</span> <span class="type">char</span>));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(d1) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(d1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(struct data) = %ld\n&quot;</span>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> data));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure><p>会看到有如下信息输出：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">sizeof(unsigned int) = 4</span><br><span class="line">sizeof(unsigned char) = 1</span><br><span class="line">sizeof(d1) = 8</span><br><span class="line">sizeof(struct data) = 8</span><br></pre></td></tr></table></figure>
              </div>
            </details></div></div></div>

<p>【注意】小端模式下，位域成员定义时，位置靠下的为高位。</p>
<h2 id="7-按位赋值"><a href="#7-按位赋值" class="headerlink" title="7.按位赋值"></a><font size=3>7.按位赋值</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 定义共用体 */</span></span><br><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">x_bit</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">char</span> m;</span><br><span class="line">    <span class="comment">/* 定义位域数据类型 */</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">data</span></span></span><br><span class="line"><span class="class">    &#123;</span></span><br><span class="line">        <span class="type">unsigned</span> <span class="type">int</span> a: <span class="number">2</span>;</span><br><span class="line">        <span class="type">unsigned</span> <span class="type">int</span> b: <span class="number">4</span>;</span><br><span class="line">        <span class="type">unsigned</span> <span class="type">int</span> c: <span class="number">2</span>;</span><br><span class="line">    &#125;d;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123; </span><br><span class="line">    <span class="class"><span class="keyword">union</span> <span class="title">x_bit</span> <span class="title">x</span>;</span><span class="comment">/* 定义共用体变量*/</span></span><br><span class="line">    x.d.a = <span class="number">2</span>;<span class="comment">/* 10  */</span></span><br><span class="line">    x.d.b = <span class="number">5</span>;<span class="comment">/* 0101*/</span></span><br><span class="line">    x.d.c = <span class="number">1</span>;<span class="comment">/* 01 */</span></span><br><span class="line">    <span class="comment">/* 01 0101 10 --&gt; 0101 0110*/</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;x.d.a = %d(%#x)\n&quot;</span>, x.d.a, x.d.a);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;x.d.b = %d(%#x)\n&quot;</span>, x.d.b, x.d.b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;x.d.c = %d(%#x)\n&quot;</span>, x.d.c, x.d.c);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;x.m = %d(%#x)\n&quot;</span>, x.m, x.m);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在终端执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 编译程序 </span><br><span class="line">./a.out          # 执行可执行文件</span><br></pre></td></tr></table></figure>

<p>会看到有如下信息输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">x.d.a = 2(0x2)</span><br><span class="line">x.d.b = 5(0x5)</span><br><span class="line">x.d.c = 1(0x1)</span><br><span class="line">x.m = 86(0x56)</span><br></pre></td></tr></table></figure>

<p>上边分析共用体的时候，我们知道刚才使用的  CPU  是小端模式，低位存放在低的地址，所以组合方式就是  01 0101 10  合并为一个字节就是  0101 0110  ，也就是  0x56  换算为十进制就是  86  了。</p>
<h1 id="七、-typedef"><a href="#七、-typedef" class="headerlink" title="七、  typedef  "></a><font size=3>七、  typedef  </font></h1><h2 id="1-什么是-typedef"><a href="#1-什么是-typedef" class="headerlink" title="1.什么是  typedef  "></a><font size=3>1.什么是  typedef  </font></h2><p>  typedef   为  C  语言的关键字，作用是为一种数据类型定义一个新名字，这里的数据类型包括内部数据类型（  int  ，  char  等）和自定义的数据类型（  struct  ）。</p>
<p>就相当于自己的名字和小名，通过  typedef  为自己的名字定义一个小名，别人叫自己小名的时候也知道叫的是自己。</p>
<p>一般来说这个关键字经常在定义结构体类型时使用，毕竟结构体的名字还是有点长的。</p>
<h2 id="2-怎么使用？"><a href="#2-怎么使用？" class="headerlink" title="2.怎么使用？"></a><font size=3>2.怎么使用？</font></h2><p>基本使用格式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> 原来数据类型 用户自定义数据类型;</span><br></pre></td></tr></table></figure>

<p>【注意】该关键字重新命名数据类型的时候，注意后边加上分号(  ;  )。</p>
<h3 id="2-1常见类型起别名"><a href="#2-1常见类型起别名" class="headerlink" title="2.1常见类型起别名"></a><font size=3>2.1常见类型起别名</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="type">unsigned</span> <span class="type">long</span> uint32;</span><br><span class="line">uint32 a = <span class="number">0</span>;</span><br><span class="line"><span class="comment">/* 上边两句就等价于 */</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">long</span> a = <span class="number">0</span>;</span><br></pre></td></tr></table></figure>

<h3 id="2-2结构体类型起别名"><a href="#2-2结构体类型起别名" class="headerlink" title="2.2结构体类型起别名"></a><font size=3>2.2结构体类型起别名</font></h3><p>一般来说，该关键字多会用在结构体中，例如，</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> _<span class="title">node_</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">     <span class="type">int</span>  data;</span><br><span class="line">     <span class="class"><span class="keyword">struct</span> _<span class="title">node_</span>  *<span class="title">next</span>;</span></span><br><span class="line">&#125; listnode, *linklist;</span><br></pre></td></tr></table></figure>

<p>这里定义了两个新的数据类型  listnode  和  linklist  。其中  listnode  等价于数据类型  struct <em>node</em>   ，而   linklist  等价于  struct <em>node</em> *  。</p>
<h3 id="2-3数组类型起别名"><a href="#2-3数组类型起别名" class="headerlink" title="2.3数组类型起别名"></a><font size=3>2.3数组类型起别名</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="type">char</span> ARRAY_20[<span class="number">20</span>];</span><br></pre></td></tr></table></figure>

<p>它表示   ARRAY_20   是与类型  char [20]  等价。它是一个长度为   20   的数组类型。接着可以用   ARRAY_20   定义数组，例如</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ARRAY_20 a1, a2;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 等价于 */</span></span><br><span class="line"><span class="type">char</span> a1[<span class="number">20</span>], a2[<span class="number">20</span>];</span><br></pre></td></tr></table></figure>

<h3 id="2-4数组指针类型起别名"><a href="#2-4数组指针类型起别名" class="headerlink" title="2.4数组指针类型起别名"></a><font size=3>2.4数组指针类型起别名</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="title function_">int</span> <span class="params">(*PTR_TO_ARR)</span>[4];</span><br></pre></td></tr></table></figure>

<p>这里定义了一个新的数据类型  PTR_TO_ARR  ，它等价于类型  int * [4]  ，它是一个二维数组指针类型。接着可以使用   PTR_TO_ARR   定义二维数组指针，例如</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">PTR_TO_ARR a1, a2;</span><br></pre></td></tr></table></figure>

<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">typedef</span> <span class="title function_">char</span> <span class="params">(*PTR_TO_ARR)</span>[30];</span><br><span class="line"><span class="type">char</span> str[<span class="number">3</span>][<span class="number">30</span>] =</span><br><span class="line">&#123;</span><br><span class="line">	<span class="string">&quot;fanhua_0&quot;</span>,</span><br><span class="line">	<span class="string">&quot;fanhua_1&quot;</span>,</span><br><span class="line">	<span class="string">&quot;fanhua_2&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	PTR_TO_ARR parr = str;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;<span class="number">3</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;str[%d]: %s\n&quot;</span>, i, *(parr+i));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令编译程序：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out </span><br><span class="line">./a.out # 执行可执行程序</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">str[0]: fanhua_0</span><br><span class="line">str[1]: fanhua_1</span><br><span class="line">str[2]: fanhua_2</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-5函数指针起别名"><a href="#2-5函数指针起别名" class="headerlink" title="2.5函数指针起别名"></a><font size=3>2.5函数指针起别名</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="title function_">int</span> <span class="params">(*PTR_TO_FUNC)</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line">PTR_TO_FUNC pfunc;</span><br></pre></td></tr></table></figure>

<p>这里定义了一个新的类型  PTR_TO_FUNC  ，也就是函数指针类型。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">typedef</span> <span class="title function_">int</span> <span class="params">(*PTR_TO_FUNC)</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">max</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b ? a : b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	PTR_TO_FUNC pfunc = max;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;max: %d\n&quot;</span>, (*pfunc)(<span class="number">5</span>, <span class="number">6</span>));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>在终端执行以下命令编译程序：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out </span><br><span class="line">./a.out # 执行可执行程序</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">max: 6</span><br></pre></td></tr></table></figure>
              </div>
            </details>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/f72c7684.html">LV01-15-C语言-自定义数据类型</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年03月04日 - 15:08</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/f72c7684.html" title="LV01-15-C语言-自定义数据类型">https://sumumm.github.io/post/f72c7684.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV01-C语言基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/342e4ba9.html" rel="prev" title="LV01-16-C语言-内存管理">
                  <i class="fa fa-angle-left"></i> LV01-16-C语言-内存管理
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/73483bf2.html" rel="next" title="LV01-14-C语言-预处理">
                  LV01-14-C语言-预处理 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
